I'm using many "entry type" templates as described here: http://buildwithcraft.com/help/entry-type-templates. I'm using ElementAPI to render full entries as HTML embedded into JSON. I'm using this transformer method:

craft()->templates->render('path/to/template/template-name', [
    'entry' => $template

This works fine. But I need to assign the corresponding template to each entry-type. I have not found a solution. I can't seem to access the name or path of the assigned template. I don't really need the path, but need the name. The following seems right, but doesn't work:

'path/to/template/' . $template->getTemplateName()

Any ideas??


  • What type of object is $template? getTemplateName() won't work unless it's an actual \Twig_Template class.
    – Brad Bell
    Jan 5 '16 at 20:49

Perhaps instead of trying to get a template name, use the entry type as the template name, similar to the method that you linked to. In the link, it doesn't check which template is assigned to an entry, but instead, uses the entry type to load a template with the same name. In your example that might look like this:

craft()->templates->render('path/to/template/' . $entry.type, [
  'entry' => $entry

Where your template would be named the same as the handle for your entry type. Does that make sense?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.