• Development
  • Is it possible to pass a variable to a snippet?

Can I pass a variable to a snippet? Something like this:

<@ snippets/sign-up-block.php {myVariable: 'test'} @>

The usecase for this is I have an UI component with input element that has id attribute. I want to use the same ui component on the same page multiple times, so I need to make sure the id is different for every one of them. Passing a variable into it can solve my problem. Or maybe there's a better way to handle this?

  • mad replied to this.

    inxeption_dev

    Hi,
    you can use set for this. Assuming you have a snippet called snippets/file.php with the following content:

    <p>@{ var }</p>

    Then use set before calling the snippet in the main template as follows:

    <@ set { var: 'test' } @> 
    <@ snippets/file.php @> 

    And when calling the snippet a second time, you can just set another value for the variable before again.

    You're welcome! ?

    2 years later

    Hi Im struggling with the same issue here 😢

    As I see it i can pass a different variable (the text) for each repeated snippet by setting the variable in the code.

    Using this method I can see the different variables passed in as content, yet if I edit the text as content, they still revert to all being the same content.

    How does this help when AFAIK the concept of using the text varaiable was to make content editable.

    Or am i doing this wrong?

    • mad replied to this.

      maakbow Not sure if I understand correctly, but you can set different text variables that can be edited very easy as follows:

      <@ set { :temp: @{ text1 } } @> 
      <@ snippets/file.php @>    
      <@ set { :temp: @{ text2 } } @> 
      <@ snippets/file.php @>    

      In the snippet file just use the @{ :temp } variable.

      Thanks.
      Do i actually need to use the "temp" word?. Even so ...

      as a test I put <p> PH:@{ :temp } in my snipet </p>

      and

      <@ set { :temp: @{phoneNum-1} } @>
      <@ snippets/testBlock.php @>
      <hr>
      <@ set { :temp: @{phoneNum-2} } @>
      <@ snippets/testBlock.php @>
      <hr>
      <@ set { :temp: @{phoneNum-3} } @>
      <@ snippets/testBlock.php @>

      in the page.

      Using :temp with the colon as you have written does not work at all. No edit buttons appear in page.

      If I take out the colon : :@{ temp } it works but were back to edit any in page and the same text appears in all.

      • mad replied to this.

        maakbow I doesn’t work like that. The colon makes a variable disappear from the user interface and should be used to temporarily store values. You have to set that temporary variable before calling the snippet. The other variables for the phone numbers can be changed by the user in the dashboard. The in-page edit mode only shows editable fields for variables that are directly used in the template. Variables in set are ignored and can only be changed in the dashboard.

          Newest Discussions