1

I'm trying to render my HTML, CSS, JavaScript with the use of template hooks from a plugin.

{% hook 'video.slider' %}

Is it possible? If it is, can you give some snippet?

1 Answer 1

6

In your plugin's main file init method setup your template hook. In this hook you can call a service method to return your rendered HTML.

public function init()
    {
        craft()->templates->hook('video.slider', function(&$context)
        {
            $response = craft()->pluginname_service->serviceMethod();
            return $response;
        });
    }

In your plugin's service file create a method to return your rendered HTML.

function serviceMethod() 
    {   

        $oldPath = craft()->path->getTemplatesPath();

        $newPath = craft()->path->getPluginsPath().'path/templates/folder';
        craft()->path->setTemplatesPath($newPath);

        $templateName = '_templatename';

        //If you need to get data from somewhere else to pass to the template
        $htmlData = craft()->plugin_service->serviceMethod();

        $htmlResponse = craft()->templates->render($templateName, array("variableName" => $htmlData));

        //reset path
        craft()->path->setTemplatesPath($oldPath);

        return $htmlResponse;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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