Set attributes in haml using variable in mustache

2019-09-01 06:51发布

问题:

I'm using both haml and mustache for frontend. There is a code snippet:

.module-subtitle
  {{title}}

I want to show a tooltip for .module-subtitle with title attribute, using the content inside {{title}}. I tried

.module-subtitle{ :"title" => {{title}}}
  {{title}}

but it didn't work as it has syntax error. Any hints?

回答1:

You could use :plain, something like this:

:plain
  <div class="module-subtitle" title="{{title}}">
    {{title}}
  </div>


回答2:

Without seeing more of your code and running some experiments, I would guess initially that it's the order of the template rendering. If Haml is rendering first, then it will not like .module-subtitle{ :"title" => {{title}}}. If Moustache runs first, it should replace .module-subtitle{ :"title" => {{title}}} with .module-subtitle{ :"title" => YourTitle} but note also that in this case YourTitle is not string delimited.

If your object is available in the haml rendering context, then you could leave it to haml to render? .module-subtitle{ title: my_object.title}