Ad

Can I Pass A Dynamic Value From Craft CMS To Another Template As Well As A String Value?

- 1 answer

Can I pass a dynamic value from craft CMS using twig to another template as well as a string value? Using the following gives me a syntax error

"Twig_Error_Syntax: A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "," ("punctuation" expected with value ":") "

        {% set myEntry= craft.entries.slug('myEntry').first %}
        {% embed '_layouts/_hero' with {
            'extraClasses': "{{ myEntry.featuredBgPosition }} hero--medium",
            'heroImage': myEntry.featuredImage.first
        } %}

Other answers say to not use curly braces but this doesn't work either

'extraClasses': myEntry.featuredBgPosition "hero--medium",

and when using it within the quotations I get the string myEntry.featuredBgPosition as a class

'extraClasses': myEntry.featuredBgPosition "hero--medium",

Ad

Answer

The answers that suggest removing the curly braces are correct, but you need to concatentate strings with variables.

        {% set myEntry= craft.entries.slug('myEntry').first %}
        {% embed '_layouts/_hero' with {
            'extraClasses': myEntry.featuredBgPosition ~ " hero--medium",
            'heroImage': myEntry.featuredImage.first
        } %}
Ad
source: stackoverflow.com
Ad