Skip to content
Snippets Groups Projects
Select Git revision
  • f530ef20c0f55cbc17f81d9f3468f6152c416341
  • main default protected
  • studip-rector
  • ci-opt
  • course-members-export-as-word
  • data-vue-app
  • pipeline-improvements
  • webpack-optimizations
  • rector
  • icon-renewal
  • http-client-and-factories
  • jsonapi-atomic-operations
  • vueify-messages
  • tic-2341
  • 135-translatable-study-areas
  • extensible-sorm-action-parameters
  • sorm-configuration-trait
  • jsonapi-mvv-routes
  • docblocks-for-magic-methods
19 results

PhpTemplate.php

Blame
  • Forked from Stud.IP / Stud.IP
    1402 commits behind the upstream repository.
    Jan-Hendrik Willms's avatar
    Jan-Hendrik Willms authored
    Closes #4228
    
    Merge request studip/studip!3059
    f530ef20
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    PhpTemplate.php 3.45 KiB
    <?php
    /**
     * A template engine that uses PHP to render templates.
     *
     * @copyright 2008 Marcus Lunzenauer <mlunzena@uos.de>
     * @author Marcus Lunzenauer <mlunzena@uos.de>
     * @license MIT
     */
    
    namespace Flexi;
    
    class PhpTemplate extends Template
    {
        /**
         * Parse, render and return the presentation.
         *
         * @return string A string representing the rendered presentation.
         * @throws TemplateNotFoundException
         */
        protected function _render(): string
        {
            extract($this->get_attributes());
    
            # include template, parse it and get output
            ob_start();
            require $this->template;
            $content_for_layout = ob_get_contents();
            ob_end_clean();
    
            # include layout, parse it and get output
            if (isset($this->layout)) {
                $defined = get_defined_vars();
                unset($defined['this']);
                $content_for_layout = $this->layout->render($defined);
            }
    
            return $content_for_layout;
        }
    
        /**
         * Parse, render and return the presentation of a partial template.
         *
         * @param Template|string $partial A partial name or template
         * @param array $attributes An optional associative array of attributes
         *                          and their associated values.
         * @return string A string representing the rendered presentation.
         * @throws TemplateNotFoundException
         */
        public function render_partial(Template|string $partial, array $attributes = []): string
        {
            return $this->factory->render($partial, $attributes + $this->attributes);
        }
    
        /**
         * Renders a partial template with every member of a collection. This member
         * can be accessed by a template variable with the same name as the name of
         * the partial template.
         *
         * Example:
         *
         *   # template entry.php contains:
         *   <li><?= $entry ?></li>
         *
         *
         *   $entries = ['lorem', 'ipsum'];
         *   $template->render_partial_collection('entry', $entries);
         *
         *   # results in:
         *   <li>lorem</li>
         *   <li>ipsum</li>
         *
         * If you want to use specific content between the rendered partials, you
         * may define a spacer partial that will be used for that. The spacer will
         * be rendered with the given attributes.
         *
         * @param string $partial A name of a partial template.
         * @param array $collection The collection to be rendered.
         * @param Template|string|null $spacer Optional a name of a partial template
         *                                     used as spacer.
         * @param array $attributes An optional associative array of attributes
         *                          and their associated values.
         *
         * @return string A string representing the rendered presentation.
         * @throws TemplateNotFoundException
         */
        public function render_partial_collection(
            string $partial,
            array $collection,
            Template|string|null $spacer = null,
            array $attributes = []
        ): string {
            $template = $this->factory->open($partial);
            $template->set_attributes($this->attributes);
            $template->set_attributes($attributes);
    
            $collected = [];
            $iterator_name = pathinfo($partial, PATHINFO_FILENAME);
            foreach ($collection as $element) {
                $collected[] = $template->render([$iterator_name => $element]);
            }
    
            $spacer = isset($spacer) ? $this->render_partial($spacer, $attributes) : '';
    
            return implode($spacer, $collected);
        }
    }