I have created an infinite loop by crossreferencing two includes to each other. On one page. My aim is to load pop up layers on a page (teachers and instruments) and have links to each other.

{#      include: _layer_instrument      #}
<a   href="javascript:void(0)" onclick = 
                        {{ teacher.title }}</a> 
{% include "includes/_layer_teacher" %} 

{#     include: _layer_teacher         #}
<a   href="javascript:void(0)" onclick = 
                        {{ instrument.title }}</a> 
{% include "includes/_layer_instrument" %}  

Is it possible to use ajax to not load the includes until they are needed?


You can create a custom plugin with Pluginfactory.io and check the box for Controllers but leave the name blank. Copy the downloaded files into your path/to/craft/craft/plugins/>>nameofyourplugin<</ directory and install it in your cp.

Then you'll insert a function like this in your controller

public function actionRenderTemplate(){
    $id = craft()->request->getParam('id');
    // do some checks if the id is valid or not here...

    $html = '';
    $success = false;
    $message = '';

    if($entry = craft()->entries->getEntryById($id)){
        // as example to render different templates based on entry type
        $entryType = $entry->getType()->handle; 
        $html = craft()->templates->render('path/to/your/template/' . $entryType, array(
            'entry' => $entry
            $success = true;
        $message = Craft::t('could not find entry with id ' . $id);

        'message'   => $message,
        'success'   => $success,
        'html'      => $html

And your Javascript could be this

    var data = {
        action: "pluginname/actionRenderTemplate",
        id: $(this).data('id')
        type: "post",
        url: '',
        data: data,
        success: function(data){
            if(data.success === true){
                // display error messages here
        error: function (XMLHttpRequest, textStatus) {
            console.log("Status: " + textStatus);

However I would recommend you to read the docs about Controllers and about templates to fully understand what you have to do here

edit: your html could be

<button data-id="{{instrument.id}}" class="load-instrument" value="click me">Click me</button>
  • Thanks Robin! Wow! Let's see if I get this sorted. – analog junkie Jan 27 '18 at 12:00
  • Just tell me if you have any questions. If it's totally new to you and you can't solve something we can go to the chat as well. – Robin Schambach Jan 27 '18 at 12:02
  • Yes, it is definitely above my level of skills. How do I implement the plugin into the site and how does it load the include? – analog junkie Jan 28 '18 at 17:15
  • Unfortunately you can't use the chat yet (you need 20 reputation points) so we have to stay here until you got them. I provided a few links, the question how to implement the plugin is explained in the first one. Create a new folder in your craft/plugins directory and set the name of the folder how you named the plugin in pluginfactory.io (all lower case) and copy the downloaded files. In your cp you have settings -> plugins you'll need to install the plugin there. As for the include, you won't have an include anymore since you'll load your template in your controller and not in twig anymore – Robin Schambach Jan 28 '18 at 20:37
  • Making and installing the plugin was fine. Now I need to fiddle how to get the template to work. How do i call the id in the plugin, like I did it in the "onclick"-script (document.getElementById('{{teacher}}') to get the right data from craft I am calling in the for loop. I am very fresh to building websites, so I am leaning out of the window a lot (as one would say in german ;-)) and I am very grateful for your help. – analog junkie Jan 29 '18 at 7:51

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.