Skip to content

Commit

Permalink
Added embed_design_file template operator for embedding file content
Browse files Browse the repository at this point in the history
This operator can be used to embed the contents of files located
in the design folders, the design folders are searched until
a matching file is found.
See doc/swark.rst for more details.
  • Loading branch information
am0s committed Dec 16, 2019
1 parent f54827b commit ab37231
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
6 changes: 6 additions & 0 deletions autoloads/classes/Exceptions/TemplateResourceNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Swark\Exceptions;

class TemplateResourceNotFound extends \Exception
{
}
91 changes: 91 additions & 0 deletions autoloads/classes/TemplateOperators/EmbedDesignFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
namespace Swark\TemplateOperators;

use eZSys;
use eZTemplateDesignResource;
use eZTemplate;
use SwarkOperator;
use Swark\Exceptions\TemplateResourceNotFound;

/**
* A template operator which allows for embedding files found in the 'design' folders
* of any extension. This is similar to the `ezdesign` operator but will embed
* the contents of the file.
*
* The file is specified using the relative path from a design folder and the
* operator will find the first file that matches according to the design order.
* For instance `javascript/code.js` could be resolved to `extension/site/design/site/javscript.code.js`.
*
* If the parameter `element` is `true` then it will create the HTML element
* for embedding if the file format is known. Currently supports:
*
* - .js - Adds a <script> tag
* - .css - Adds a <style> tag
*/
class EmbedDesignFile extends SwarkOperator
{
function __construct()
{
parent::__construct('embed_design_file', 'path', 'element=');
}

/**
* Locates the file in the design folders and returns the file path.
*
* @param eZTemplate $tpl Template instance
* @param string $designPath Relative path to file, e.g. 'javascript/code.js'
* @return void
*/
static public function findDesignFile($tpl, $designPath)
{
$sys = eZSys::instance();

$bases = eZTemplateDesignResource::allDesignBases();
$triedFiles = array();
$fileInfo = eZTemplateDesignResource::fileMatch($bases, false, $designPath, $triedFiles);

if ( !$fileInfo )
throw new TemplateResourceNotFound($designPath);

$filePath = $fileInfo['path'];

return $filePath;
}

static public function execute($operatorValue, $namedParameters)
{
$tpl = eZTemplate::factory();
$designPath = $namedParameters['path'];
list($path, $text) = self::loadFile($tpl, $designPath);
if ($namedParameters['element']) {
if ($path) {
if (substr($path, -3) == ".js") {
$text = "<script type=\"text/javascript\">$text</script>";
} else if (substr($path, -4) == ".css") {
$text = "<style>$text</style>";
}
}
}
return $text;
}

/**
* Load file content and return an array with file path and content.
*
* @param eZTemplate $tpl Template instance
* @param string $designPath Relative path to file, e.g. 'javascript/code.js'
* @throws TemplateResourceNotFound if the template resource could not be found
* @return array
*/
static public function loadFile($tpl, $designPath)
{
try {
$path = self::findDesignFile($tpl, $designPath);
} catch (TemplateResourceNotFound $e) {
// starter_logger("site")->error("template_embed_file: Failed to find template resource: $designPath");
\eZDebug::writeError("template_embed_file: Failed to find template resource: $designPath");
return array(null, '');
}
return array($path, file_get_contents($path));
}
}
20 changes: 20 additions & 0 deletions doc/swark.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,26 @@ Workflow event types
.. include:: eventtypes/defertocron.rst


Embed design file
=================

`embed_design_file` embeds a file located in the designed folders on the
site. This is similar to th `ezdesign` operator but will return the
contents of the file instead of the path.

This can for instance be used to embed javascript code from a file,
the javascript file can then be totally separate from the template code
and can be opened like a normal javascript file. Another use case is
to embed handlebar templates.

Pass the relative file path to the operator, for instance `javascript/code.js`
could be resolved to `extension/site/design/site/javscript.code.js`.

If the second parameter is used and set to true then the returned value
will contain an HTML element around the file contents if the file type
is known, currently only Javascript and CSS files are supported.


Custom operators
================

Expand Down
1 change: 1 addition & 0 deletions settings/swark.ini
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ OperatorMap[variable_names]=SwarkVariableNamesOperator
OperatorMap[get_scheme]=SwarkGetSchemeOperator
OperatorMap[url_modify]=Swark\TemplateOperators\UrlModifier
OperatorMap[ezselection_content]=Swark\TemplateOperators\EzSelectionContent
OperatorMap[embed_design_file]=Swark\TemplateOperators\EmbedDesignFile

0 comments on commit ab37231

Please sign in to comment.