DumbDevGirl42069
DumbDevGirl42069

Reputation: 959

How can I pass a dynamic value to another component in twig/ craft cms?

I need the title to show the (current date - 1) When I hard code a value eg "17"

This is where the component is displaying (in index)

   {% include 'home/key-facts' with {
        content: {
            keyFactsHeading: entry.keyFactsHeading,
            keyFacts: entry.keyFacts,
            keyFactsSmall: entry.keyFactsSmall,

        }

Which is this file here ---> This is how I've included the date

{% include '_components/bg-type' with {
                        content: {
                            title:  {{ "now"|date('Y') - 1 }} 
           },
} only %}

I am passing content.title into here --->

<div class="bg-type">
    <div class="bg-type__text bg-type--large">
        {{ content.title }}
    </div>
</div>

When hardcoding the value as below it works fine but when I add
title: {{ "now"|date('Y') - 1}} I get a 500 error.

 {% include '_components/bg-type' with {
                    content: {
                        title:  17

       },
    } only %}

Why is this? Can you also explain why what I'm trying doesn't work? I've tried dumping {{ "now"|date('Y') - 1}} and I can see the year I want

Upvotes: 0

Views: 374

Answers (1)

DarkBee
DarkBee

Reputation: 15625

The {{ ... }} notation is used to output data. In this case you only want to pass data towards an include. Notice you already inside a twig-statement, {% include .... %}

The correct syntax would be

{% include '_components/bg-type' with {
    content: {
        title:  "now"|date('Y') - 1,
   },
} only %}

Upvotes: 1

Related Questions