3

I'm using underscorejs function _.template() to render html on client side. I found that in many examples, the templates are placed in some <script type="text/template" id="id"> on the page, and using $('#id') to get the templates. like this:

<script type="text/template" id="id">
  <div class="form-group row">
    <label for="nickname" class="col-sm-2 control-label text-right">nickname</label>
    <div class="col-sm-10"><span><%= data.subscriber.nickname %></span></div>
  </div>
  <div class="form-group row">
    <label for="sex" class="col-sm-2 control-label text-right">sex</label>
    <div class="col-sm-10"><span><%= data.subscriber.sex %></span></div>
  </div>
  <div class="form-group row">
    <label for="country" class="col-sm-2 control-label text-right">country</label>
    <div class="col-sm-10"><span><%= data.subscriber.country %></span></div>
  </div>
</script>

I try build a block to hold these tags, but drupal will break it, lost some tags eventually.

My solution right now drives me crazy: save the template as one line string or concat multiline strings with \ in js.

I really need a better idea. Thanks.

UPDATE: I found a better idea, using coffee script raw string. From External template in Underscore

1
  • 1
    Sadly, it seems that Drupal manages only css, js, and libraries. And libraries have css and js hardcoded - no flexibility to add anything else. Would love it to change from 'js' to 'script', but it will not happen in 7, I doubt it could happen in 8, and it's long wait for 9. So all we can get is someone hacking around this limitation. I'd like to see it happen.
    – Mołot
    Commented Jan 2, 2014 at 8:29

2 Answers 2

1

You really shouldn't be putting things like that in a drupal block via the UI anyway because someone with permissions could come along and mess it up and then everything breaks.

You can add/remove/change markup on the page by editing the templates in your theme.

For example you could add your template script tag into the page.tpl.php file of your theme.

Assuming you are using the same template on all pages (or a bunch of pages) or you just want a template on a specific page then this is really straight forward.

If you have more complex requirements you might need a large amount of template overrides or you may want to use some code in template preprocess functions in template.php to make things simpler.

For more information on this see Theming Drupal 6 and 7. There is a lot of info in there.

1

New solution:

Create a new format without any html filter at admin/config/content/formats. Then good to go :)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.