0

I want to use the 'category' variable which is used in catalog/view/theme/theme_name/template/product/category.twig in another twig template I am editing but I can't figure out how to use it in the file.

I added the following code to the twig template I'm trying to work on to see which variables were available to the template:

<ol>
    {% for key, value in _context  %}
      <li>{{ key }}</li>
    {% endfor %}
</ol>

However, the 'category' variable wasn't listed on the page, only the 'category_text' variable. How can I use the category variable in my twig template?

Does anyone know how to do this? Thanks for the help!

Ken White
  • 123,280
  • 14
  • 225
  • 444
LostThyme
  • 23
  • 5
  • You should add to your question what you have done it is mean you should add your code what you have tried and where you have an issue. Error log and so on. – K. B. Apr 24 '20 at 11:23
  • @K.B. Thanks for letting me know! I've tried to add more to my question - hope that helps. – LostThyme Apr 24 '20 at 12:01
  • `_context` must be declared in corresponding controller file something like that: `$data['_context'] = $context_array;` You did it? – K. B. Apr 24 '20 at 12:21
  • I've rolled back your edit. It's inappropriate here to add (SOLVED) to the title and edit a solution into the question itself. If you've found a solution and want to share it, do so by writing an answer below in the space provided for that purpose. See [Can I answer my own question?](http://stackoverflow.com/help/self-answer) for more information. – Ken White Apr 24 '20 at 20:57

1 Answers1

0

Different twig files are rendered with different variable through controller. So it's not that you would pass a variable from one twig file to another twig file. That would be-

  1. Controller (PHP File) will be holding the value (may be fetching from database) and passing to the twig1 template, and you need to get that variable in another controller function which provides value to second twig2. If controllers are same and variables are passed to both or multiple twigs from the same controller then variables can be easily used in the twig.

  2. If you are finding it difficult javascript in frontend can do the job for you. This link could be useful to you for javascript related solution.

Vishal Kumar Sahu
  • 1,232
  • 3
  • 15
  • 27