Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-factorin committed Jul 18, 2018
0 parents commit daadee8
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea/
/vendor/
composer.lock
16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "slimcake/templates",
"description": "Additional templating classes for slimcake framework",
"license": "MIT",
"require": {
"php": ">=5.3"
},
"require-dev": {
"twig/twig": "^v1.30"
},
"autoload": {
"psr-4": {
"Slimcake\\Templates\\": "src/"
}
}
}
27 changes: 27 additions & 0 deletions src/TemplateView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Slimcake\Templates;

use Slimcake\Core\View;

/**
* Class TemplateView
* @package Slimcake\Templates
*/
class TemplateView extends View
{
/** @var string $template */
protected $template;

/**
* @return mixed|string
* @throws \Slimcake\Core\Exception
*/
public function build()
{
$this->set('__contents__', parent::build());
$this->render($this->template);

return $this->build();
}
}
55 changes: 55 additions & 0 deletions src/TwigView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Slimcake\Templates;

use Slimcake\Core\Controller;
use Slimcake\Core\Exception;
use Slimcake\Core\View;

/**
* Class TwigView
* @package Slimcake\Templates
*/
class TwigView extends View
{
const EXTENSION = 'twig';

/** @var \Twig_Environment $twig */
protected $twig;

/**
* @return \Twig_Environment
*/
protected function getTwig()
{
return $this->twig;
}

/**
* @return string
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
*/
protected function extract()
{
return $this->twig->render($this->render, $this->data);
}

/**
* TwigView constructor.
* @param Controller $controller
* @throws Exception
*/
public function __construct(Controller $controller)
{
parent::__construct($controller);

if (class_exists(\Twig_Environment::class) === false) {
throw new Exception('Twig library is not installed');
}

$loader = new \Twig_Loader_Filesystem(sprintf('%s/', $this->path));
$this->twig = new \Twig_Environment($loader);
}
}

0 comments on commit daadee8

Please sign in to comment.