在模板调用中引用变量 - 很明显

Referencing variable inside template call - sightly

我正在尝试在 data-sly-template 调用中调用引号内的变量。我希望输出包含我的字符串,并在引号内包含适当的变量。下面是模板和我用来调用它的字符串:

模板:

    <!-- template -->
    <template data-sly-template.bnrButton="${ @ style='String: button color class (default btn-primary)', classes='String: additional component button classes', text='String: button text (translation key)', defaultText='String: default button text (translation key)', link='String: link target for button (# is default)', fullWidth='Boolean: whether to display button in full width of container', noFollow='Boolean: if button should have a nofollow tag (no is def)', gaCategory='String: GA Category', gaEvent='String: GA Event', gaLabel='String: GA Label', gaAction='String: GA Action'}">
    <a class="btn ${style || 'btn-primary'} hidden-print ${classes}${fullWidth ? ' btn-full-width' : ''}" role="button" href="${link || '#'}" rel="${noFollow ? 'nofollow' : ''}" onclick="${gaEvent @ context = 'scriptString'}">${text || defaultText  @ i18n}</a>    
    </template> 

    <!-- call made to the template -->
    <sly data-sly-call="${patterns.bnrButton @ style='btn-secondary-light', classes='btn-sm', text=teaser.libraryButtonHeading, defaultText='View Library', link=teaser.myLibraryLink.href, fullWidth='true', newWindow='false', gaEvent='trackEvent(&#39;Test 1&#39;,&#39;Test 2&#39;,&#39;${teaser.teaserMessage}&#39;)'}"></sly>

If 尝试了多种组合来获得 teaser.teaserMessage 来打印创作的消息,但一直有问题,因为模式调用已经在引号内。我希望能够将单个字符串(在模板调用中调用)传递给包含 onclick 所需的所有信息的模板。

想法?

谢谢。

您可以在模板调用之前使用 data-sly-test.variable_name 将值存储到变量中:

<sly data-sly-test.gaEvent="${'trackEvent(&#39;Test 1&#39;,&#39;Test 2&#39;,&#39;{0}&#39;)' @ format=teaser.teaserMessage}"
     data-sly-call="${patterns.bnrButton @ ..., gaEvent=gaEvent }"></sly>