Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Template Directory Timber/Twig

I have a setup a simple twig template which is used to display a simple menu. I have images in here that are static and I would like use the template directory path for the image src. However when I use {{theme.link}} it appears blank. Perhaps I'm referencing something incorrectly. Code below:

<?php 
      $context['menu'] = new TimberMenu('main-nav');
      Timber::render('templates/menu.twig', $context);
   ?>

and the twig template below:

 <ul>
    {% for item in menu.get_items %}
    <li class="{{item.classes | join(' ')}}">
      <a href="{{item.get_link}}">{{item.title}}</a>
    </li>
    {% endfor %}
 </ul>
 <img src="{{theme.link}}/assets/images/test.png" alt="">

I understand that I can pass in the directory to the context but I'm curios as to why the built in function isn't working. Probably something simple. First time looking into twig so still getting used to it. Any help greatly appreciated! Thanks

like image 713
verdond2 Avatar asked Aug 08 '16 20:08

verdond2


1 Answers

@verdond2: In order to use the {{theme}} object (and its properties) you need to start with the default Timber context in your PHP file...

<?php
  $context = Timber::get_context();
  $context['menu'] = new TimberMenu('main-nav');
  Timber::render('templates/menu.twig', $context);
  ?>
like image 167
Jared Avatar answered Sep 18 '22 05:09

Jared