{"id":19357717,"url":"https://github.com/joomla-framework/renderer","last_synced_at":"2025-04-23T11:30:32.234Z","repository":{"id":15495731,"uuid":"18229602","full_name":"joomla-framework/renderer","owner":"joomla-framework","description":"Provides a common rendering interface for PHP template engines","archived":false,"fork":false,"pushed_at":"2024-10-08T21:11:40.000Z","size":5458,"stargazers_count":7,"open_issues_count":3,"forks_count":5,"subscribers_count":10,"default_branch":"3.x-dev","last_synced_at":"2024-11-06T22:47:46.048Z","etag":null,"topics":["blade","joomla","joomla-framework","mustache","php","plates","templating","twig"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-2.1","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joomla-framework.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"joomla","custom":"https://community.joomla.org/sponsorship-campaigns.html"}},"created_at":"2014-03-28T23:27:55.000Z","updated_at":"2024-09-03T21:17:13.000Z","dependencies_parsed_at":"2024-06-19T23:17:46.647Z","dependency_job_id":"5a1c2c2d-0507-4d1f-bd09-16b5901526b6","html_url":"https://github.com/joomla-framework/renderer","commit_stats":{"total_commits":146,"total_committers":9,"mean_commits":16.22222222222222,"dds":"0.29452054794520544","last_synced_commit":"ab6b9d2af68ad29c390f9f28315cd0f0a6cc9ecc"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Frenderer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Frenderer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Frenderer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Frenderer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joomla-framework","download_url":"https://codeload.github.com/joomla-framework/renderer/tar.gz/refs/heads/3.x-dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223922122,"owners_count":17225636,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blade","joomla","joomla-framework","mustache","php","plates","templating","twig"],"created_at":"2024-11-10T07:08:57.708Z","updated_at":"2024-11-10T07:08:58.429Z","avatar_url":"https://github.com/joomla-framework.png","language":"PHP","funding_links":["https://github.com/sponsors/joomla","https://community.joomla.org/sponsorship-campaigns.html"],"categories":[],"sub_categories":[],"readme":"# The Renderer Package [![Build Status](https://ci.joomla.org/api/badges/joomla-framework/renderer/status.svg?ref=refs/heads/3.x-dev)](https://ci.joomla.org/joomla-framework/renderer)\n\n[![Latest Stable Version](https://poser.pugx.org/joomla/renderer/v/stable)](https://packagist.org/packages/joomla/renderer)\n[![Total Downloads](https://poser.pugx.org/joomla/renderer/downloads)](https://packagist.org/packages/joomla/renderer)\n[![Latest Unstable Version](https://poser.pugx.org/joomla/renderer/v/unstable)](https://packagist.org/packages/joomla/renderer)\n[![License](https://poser.pugx.org/joomla/renderer/license)](https://packagist.org/packages/joomla/renderer)\n\n## Interfaces\n\n### `Renderer\\RendererInterface`\n\n`Renderer\\RendererInterface` is an interface to provide a common rendering API.\n\n## Classes\n\n* `Renderer\\MustacheRenderer`\n* `Renderer\\PhpEngineRenderer`\n* `Renderer\\PlatesRenderer`\n* `Renderer\\TwigRenderer`\n\nAll classes except `PlatesRenderer` extend the parent rendering engine classes to enable those engines to implement the `RendererInterface`.\n\n##### Usage\n\n`Renderer\\RendererInterface` classes can be instantiated in the same manner as their parent implementations, please refer to the vendor documentation for further details.\n\nTo assist with using these classes, example service providers are shared in this repository's [samples](/samples) folder, as well as the sample configuration which these providers are dependent upon.\n\n###### Example Use Case\n\nAn example use of the `Renderer\\RendererInterface` is provided here.  In this example, our controller class builds a view class based on `Joomla\\View\\ViewInterface` and injects the required dependencies.\n\nSample Controller:\n```php\nnamespace Joomla\\Controller;\n\nuse Joomla\\Renderer\\RendererInterface;\n\nuse Joomla\\Controller\\AbstractController;\nuse Joomla\\DI\\ContainerAwareInterface;\nuse Joomla\\DI\\ContainerAwareTrait;\nuse Joomla\\View\\ViewInterface;\n\n/**\n * Default controller class for the application\n *\n * @since  1.0\n */\nclass DefaultController extends AbstractController implements ContainerAwareInterface\n{\n\tuse ContainerAwareTrait;\n\n\t/**\n\t * The default view for the application\n\t *\n\t * @var    string\n\t * @since  1.0\n\t */\n\tprotected $defaultView = 'dashboard';\n\n\t/**\n\t * State object to inject into the model\n\t *\n\t * @var    \\Joomla\\Registry\\Registry\n\t * @since  1.0\n\t */\n\tprotected $modelState = null;\n\n\t/**\n\t * Execute the controller\n\t *\n\t * This is a generic method to execute and render a view and is not suitable for tasks\n\t *\n\t * @return  boolean  True if controller finished execution\n\t *\n\t * @since   1.0\n\t * @throws  \\RuntimeException\n\t */\n\tpublic function execute()\n\t{\n\t\ttry\n\t\t{\n\t\t\t// Initialize the view object\n\t\t\t$view = $this-\u003einitializeView();\n\n\t\t\t// Render our view.\n\t\t\t$this-\u003egetApplication()-\u003esetBody($view-\u003erender());\n\n\t\t\treturn true;\n\t\t}\n\t\tcatch (\\Exception $e)\n\t\t{\n\t\t\tthrow new \\RuntimeException(sprintf('Error: ' . $e-\u003egetMessage()), $e-\u003egetCode());\n\t\t}\n\t}\n\n\t/**\n\t * Method to initialize the model object\n\t *\n\t * @return  \\Joomla\\Model\\ModelInterface\n\t *\n\t * @since   1.0\n\t * @throws  \\RuntimeException\n\t */\n\tprotected function initializeModel()\n\t{\n\t\t$model = '\\\\Joomla\\\\Model\\\\' . ucfirst($this-\u003egetInput()-\u003egetWord('view')) . 'Model';\n\n\t\t// If a model doesn't exist for our view, revert to the default model\n\t\tif (!class_exists($model))\n\t\t{\n\t\t\t$model = '\\\\Joomla\\\\Model\\\\DefaultModel';\n\n\t\t\t// If there still isn't a class, panic.\n\t\t\tif (!class_exists($model))\n\t\t\t{\n\t\t\t\tthrow new \\RuntimeException(sprintf('No model found for view %s', $vName), 500);\n\t\t\t}\n\t\t}\n\n\t\treturn new $model($this-\u003emodelState);\n\t}\n\n\t/**\n\t * Method to initialize the renderer object\n\t *\n\t * @return  RendererInterface  Renderer object\n\t *\n\t * @since   1.0\n\t * @throws  \\RuntimeException\n\t */\n\tprotected function initializeRenderer()\n\t{\n\t\t$type = $this-\u003egetContainer()-\u003eget('config')-\u003eget('template.renderer');\n\n\t\t// Set the class name for the renderer's service provider\n\t\t$class = '\\\\Joomla\\\\Service\\\\' . ucfirst($type) . 'RendererProvider';\n\n\t\t// Sanity check\n\t\tif (!class_exists($class))\n\t\t{\n\t\t\tthrow new \\RuntimeException(sprintf('Renderer provider for renderer type %s not found.', ucfirst($type)));\n\t\t}\n\n\t\t// Add the provider to the DI container\n\t\t$this-\u003egetContainer()-\u003eregisterServiceProvider(new $class);\n\n\t\treturn $this-\u003egetContainer()-\u003eget('renderer');\n\t}\n\n\t/**\n\t * Method to initialize the view object\n\t *\n\t * @return  ViewInterface  View object\n\t *\n\t * @since   1.0\n\t * @throws  \\RuntimeException\n\t */\n\tprotected function initializeView()\n\t{\n\t\t// Initialize the model object\n\t\t$model = $this-\u003einitializeModel();\n\n\t\t$view   = ucfirst($this-\u003egetInput()-\u003egetWord('view', $this-\u003edefaultView));\n\n\t\t$class = '\\\\Joomla\\\\View\\\\' . $view . 'HtmlView';\n\n\t\t// Ensure the class exists, fall back to default otherwise\n\t\tif (!class_exists($class))\n\t\t{\n\t\t\t$class = '\\\\Joomla\\\\View\\\\DefaultHtmlView';\n\n\t\t\t// If we still have nothing, abort mission\n\t\t\tif (!class_exists($class))\n\t\t\t{\n\t\t\t\tthrow new \\RuntimeException(sprintf('A view class was not found for the %s view.', $view));\n\t\t\t}\n\t\t}\n\n\t\t// HTML views require a renderer object too, fetch it\n\t\t$renderer = $this-\u003einitializeRenderer();\n\n\t\t// Instantiate the view now\n\t\t/* @type  \\Joomla\\View\\AbstractHtmlView  $object */\n\t\t$object = new $class($model, $renderer);\n\n\t\t// We need to set the layout too\n\t\t$object-\u003esetLayout(strtolower($view) . '.' . strtolower($this-\u003egetInput()-\u003egetWord('layout', 'index')));\n\n\t\treturn $object;\n\t}\n}\n```\n\nThe view class in this example is extended from this sample class which extends `\\Joomla\\View\\AbstractView`:\n```php\nnamespace Joomla\\View;\n\nuse Joomla\\Renderer\\RendererInterface;\n\nuse Joomla\\Model\\ModelInterface;\nuse Joomla\\View\\AbstractView;\n\n/**\n * Abstract HTML View class\n *\n * @since  1.0\n */\nabstract class AbstractHtmlView extends AbstractView\n{\n\t/**\n\t * The data array to pass to the renderer engine\n\t *\n\t * @var    array\n\t * @since  1.0\n\t */\n\tprivate $data = array();\n\n\t/**\n\t * The name of the layout to render\n\t *\n\t * @var    string\n\t * @since  1.0\n\t */\n\tprivate $layout;\n\n\t/**\n\t * The renderer object\n\t *\n\t * @var    RendererInterface\n\t * @since  1.0\n\t */\n\tprivate $renderer;\n\n\t/**\n\t * Class constructor\n\t *\n\t * @param   ModelInterface     $model     The model object.\n\t * @param   RendererInterface  $renderer  The renderer object.\n\t *\n\t * @since   1.0\n\t */\n\tpublic function __construct(ModelInterface $model, RendererInterface $renderer)\n\t{\n\t\tparent::__construct($model);\n\n\t\t$this-\u003esetRenderer($renderer);\n\t}\n\n\t/**\n\t * Retrieves the data array\n\t *\n\t * @return  array\n\t *\n\t * @since   1.0\n\t */\n\tpublic function getData()\n\t{\n\t\treturn $this-\u003edata;\n\t}\n\n\t/**\n\t * Retrieves the layout name\n\t *\n\t * @return  string\n\t *\n\t * @since   1.0\n\t * @throws  \\RuntimeException\n\t */\n\tpublic function getLayout()\n\t{\n\t\tif (is_null($this-\u003elayout))\n\t\t{\n\t\t\tthrow new \\RuntimeException('The layout name is not set.');\n\t\t}\n\n\t\treturn $this-\u003elayout;\n\t}\n\n\t/**\n\t * Retrieves the renderer object\n\t *\n\t * @return  RendererInterface\n\t *\n\t * @since   1.0\n\t */\n\tpublic function getRenderer()\n\t{\n\t\treturn $this-\u003erenderer;\n\t}\n\n\t/**\n\t * Method to render the view.\n\t *\n\t * @return  string  The rendered view.\n\t *\n\t * @since   1.0\n\t * @throws  \\RuntimeException\n\t */\n\tpublic function render()\n\t{\n\t\treturn $this-\u003egetRenderer()-\u003erender($this-\u003egetLayout(), $this-\u003egetData());\n\t}\n\n\t/**\n\t * Sets the data array\n\t *\n\t * @param   array  $data  The data array.\n\t *\n\t * @return  $this  Method allows chaining\n\t *\n\t * @since   1.0\n\t */\n\tpublic function setData(array $data)\n\t{\n\t\t$this-\u003edata = $data;\n\n\t\treturn $this;\n\t}\n\n\t/**\n\t * Sets the layout name\n\t *\n\t * @param   string  $layout  The layout name.\n\t *\n\t * @return  $this  Method allows chaining\n\t *\n\t * @since   1.0\n\t */\n\tpublic function setLayout($layout)\n\t{\n\t\t$this-\u003elayout = $layout;\n\n\t\treturn $this;\n\t}\n\n\t/**\n\t * Sets the renderer object\n\t *\n\t * @param   RendererInterface  $renderer  The renderer object.\n\t *\n\t * @return  $this  Method allows chaining\n\t *\n\t * @since   1.0\n\t */\n\tpublic function setRenderer(RendererInterface $renderer)\n\t{\n\t\t$this-\u003erenderer = $renderer;\n\n\t\treturn $this;\n\t}\n}\n```\n\nThe view class for our view as established in the above sample controller:\n```php\nnamespace Joomla\\View;\n\nuse Joomla\\Model\\DefaultModel;\n\n/**\n * HTML view class for the Dashboard\n *\n * @since  1.0\n */\nclass DashboardHtmlView extends AbstractHtmlView\n{\n\t/**\n\t * Redeclared model object for proper typehinting\n\t *\n\t * @var    DefaultModel\n\t * @since  1.0\n\t */\n\tprotected $model;\n\n\t/**\n\t * Method to render the view.\n\t *\n\t * @return  string  The rendered view.\n\t *\n\t * @since   1.0\n\t */\n\tpublic function render()\n\t{\n\t\t$this-\u003esetData(['data' =\u003e $this-\u003emodel-\u003egetData()]);\n\n\t\treturn parent::render();\n\t}\n}\n```\n\n## Installation via Composer\n\nYou can simply run the following from the command line:\n\n```sh\ncomposer require joomla/renderer \"~3.0\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoomla-framework%2Frenderer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoomla-framework%2Frenderer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoomla-framework%2Frenderer/lists"}