Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:46:26+00:00 2026-06-15T16:46:26+00:00

I am attempting if use a variable to call a specific macro name. I

  • 0

I am attempting if use a variable to call a specific macro name.

I have a macros file that is being imported

{% import 'form-elements.html.twig' as forms %}

Now in that file there are all the form element macros: text, textarea, select, radio etc.

I have an array variable that gets passed in that has an elements in it:

$elements = array(
    array(
        'type'=>'text,
        'value'=>'some value',
        'atts'=>null,
    ),
    array(
        'type'=>'text,
        'value'=>'some other value',
        'atts'=>null,
    ),
);

{{ elements }}

what im trying to do is generate those elements from the macros. they work just fine when called by name:

{{ forms.text(element.0.name,element.0.value,element.0.atts) }}

However what i want to do is something like this:

{% for element in elements %}
{{ forms[element.type](element.name,element.value,element.atts) }}
{% endfor %}

I have tried the following all resulting in the same error:

{{ forms["'"..element.type.."'"](element.name,element.value,element.atts) }}
{{ forms.(element.type)(element.name,element.value,element.atts) }}
{{ forms.{element.type}(element.name,element.value,element.atts) }}

This unfortunately throws the following error:

 Fatal error: Uncaught exception 'LogicException' with message 'Attribute "value" does not exist for Node "Twig_Node_Expression_GetAttr".' in Twig\Environment.php on line 541

Any help or advice on a solution or a better schema to use would be very helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:46:27+00:00Added an answer on June 15, 2026 at 4:46 pm

    I just thought other people may want the answer to this, as provide by fabpot:

    This is indeed something that is not supported: calling a macro with a dynamic name (I have added a proper exception to be clearer about the issue).

    If you really want to do that, you can do so with the following code:

    {{ attribute(forms, element.type, [element.name,element.value,element.atts]) }}

    -fabpot

    https://github.com/twigphp/Twig/issues/922#issuecomment-11133299

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In attempting to use std::select1st from <functional> in a VS2008 project I found that
I have been attempting to use ASP.NET MVC remote validation for username login access
I have written a tab based application in Xcode/RestKit and am attempting to use
I'm new to threading (and C/C++ for that matter), and I'm attempting to use
I'm in a thread. I have an address. Is that address from a variable
I am attempting to use a UIWebView to display some variable-size content, and I'd
I'm attempting to use Wix to create a database during install. I have my
I'm attempting to use the ChannelFactory to call an asynchronous service. The bit I'm
Attempting to use squarebracket notation to reference a dynamic variable. (I'm looping through a
I am attempting to use a variable to collect information from an object. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.