Browse files

Made it possible to store the snippets in a file.

See the examples in triton.
  • Loading branch information...
1 parent 995d66b commit 054612bda3d40cc74a0ad7ab5fa85f825e236a94 @tijsverkoyen tijsverkoyen committed Jan 5, 2012
View
61 backend/core/ajax/templates.php
@@ -23,7 +23,7 @@ public function execute()
parent::execute();
// init vars
- $templates = '';
+ $templates = array();
$theme = BackendModel::getModuleSetting('core', 'theme');
$files[] = BACKEND_PATH . '/core/layout/editor_templates/templates.js';
$themePath = FRONTEND_PATH . '/themes/' . $theme . '/core/layout/editor_templates/templates.js';
@@ -33,8 +33,8 @@ public function execute()
// loop all files
foreach($files as $file)
{
- // process file
- $templates[] = $this->processFile($file);
+ // process file
+ $templates = array_merge($templates, $this->processFile($file));
}
// set headers
@@ -44,7 +44,7 @@ public function execute()
if(!empty($templates))
{
echo 'CKEDITOR.addTemplates(\'default\', { imagesPath: \'/\', templates:' . "\n";
- echo '[' . implode(',' . "\n", $templates) . ']' . "\n";
+ echo json_encode($templates) . "\n";
echo '});';
}
exit;
@@ -54,29 +54,56 @@ public function execute()
* Process the content of the file.
*
* @param string $file The file to process.
- * @return boolean|string
+ * @return boolean|array
*/
private function processFile($file)
{
// if the files doesn't exists we can stop here and just return an empty string
- if(!SpoonFile::exists($file)) return '';
+ if(!SpoonFile::exists($file)) return array();
// fetch content from file
- $content = SpoonFile::getContent($file);
+ $content = SpoonFile::getContent($file);
+ $json = @json_decode($content, true);
- $search = array("\n", "\r", "\t", 'image: "/', 'image: \'/');
- $replace = array('', '', '', 'image: "', 'image: \'');
+ // skip invalid JSON
+ if($json === false) return array();
+ if($json === null) return array();
- // replace some stuff, we need to replace the first slash for images, because we will set it in the config
- // it ourself, otherwise no images will be shown
- $content = str_replace($search, $replace, $content);
+ $return = array();
- // remove array stuff
- $content = trim($content, '[]');
+ // loop templates
+ foreach($json as $template)
+ {
+ // skip items without a title
+ if(!isset($template['title'])) continue;
+
+ if(isset($template['file']))
+ {
+ if(SpoonFile::exists(PATH_WWW . $template['file']))
+ {
+ $template['html'] = SpoonFile::getContent(PATH_WWW . $template['file']);
+ }
+ }
+
+ // skip items without HTML
+ if(!isset($template['html'])) continue;
- // split the templates
- $content = str_replace('},', "},\n", $content);
+ $image = '';
+ if(isset($template['image']))
+ {
+ // we have to remove the first slash, because that is set in the wrapper. Otherwise the images don't work
+ $image = ltrim($template['image'], '/');
+ }
+
+ $temp['title'] = $template['title'];
+ $temp['description'] = (isset($template['description'])) ? $template['description'] : '';
+ $temp['image'] = $image;
+ $temp['html'] = $template['html'];
+
+ // add the template
+ $return[] = $temp;
+ }
- return $content;
+ return $return;
}
}
View
16 backend/core/layout/editor_templates/templates.js
@@ -1,14 +1,14 @@
[
{
- title: 'Left aligned image',
- image: '/backend/core/layout/editor_templates/images/left_aligned_image.gif',
- description: 'A paragraph of text with an image on the left.',
- html: '<p><img class="alignLeft" src="/backend/core/layout/editor_templates/images/left_aligned_image.gif" height="70" width="100" />Type the text here</p>'
+ "title": "Left aligned image",
+ "image": "/backend/core/layout/editor_templates/images/left_aligned_image.gif",
+ "description": "A paragraph of text with an image on the left.",
+ "html": "<p><img class=alignLeft src=/backend/core/layout/editor_templates/images/left_aligned_image.gif height=70 width=100 />Type the text here</p>"
},
{
- title: 'Right aligned image',
- image: '/backend/core/layout/editor_templates/images/right_aligned_image.gif',
- description: 'A paragraph of text with an image on the right.',
- html: '<p><img class="alignLeft" src="/backend/core/layout/editor_templates/images/right_aligned_image.gif" height="70" width="100" />Type the text here</p>'
+ "title": "Right aligned image",
+ "image": "/backend/core/layout/editor_templates/images/right_aligned_image.gif",
+ "description": "A paragraph of text with an image on the right.",
+ "html": "<p><img class=alignLeft src=/backend/core/layout/editor_templates/images/right_aligned_image.gif height=70 width=100 />Type the text here</p>"
}
]
View
BIN frontend/themes/triton/core/layout/editor_templates/images/chuck.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN frontend/themes/triton/core/layout/editor_templates/images/chuck_norris.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
4 frontend/themes/triton/core/layout/editor_templates/in_a_file.html
@@ -0,0 +1,4 @@
+<p>
+ <img src="/frontend/themes/triton/core/layout/editor_templates/images/chuck_norris.jpg" width="100" height="100" alt="Chuck Norris" class="alignLeft" />
+ This is Chuck Norris, he can do a wheelie on a unicycle.
+</p>
View
14 frontend/themes/triton/core/layout/editor_templates/templates.js
@@ -1,8 +1,14 @@
[
{
- title: 'A tribute to Fork',
- image: '/frontend/themes/triton/core/layout/editor_templates/images/icon.png',
- description: '',
- html: '<p><img class="alignLeft" src="/frontend/themes/triton/core/layout/editor_templates/images/icon.png" height="70" width="100" />This theme was made for <a href="http://www.fork-cms.com">Fork</a>.</p>'
+ "title": "A tribute to Fork",
+ "description": "This is an example snippet.",
+ "image": "/frontend/themes/triton/core/layout/editor_templates/images/icon.png",
+ "html": "<p><img class=\"alignLeft\" src=\"/frontend/themes/triton/core/layout/editor_templates/images/icon.png\" height=\"70\" width=\"100\" />This theme was made for <a href=\"http://www.fork-cms.com\">Fork</a>.</p>"
+ },
+ {
+ "title": "Fact about Chuck Norris",
+ "description": "The content of this snippet is stored in a file.",
+ "image": "/frontend/themes/triton/core/layout/editor_templates/images/chuck.png",
+ "file": "/frontend/themes/triton/core/layout/editor_templates/in_a_file.html"
}
]

0 comments on commit 054612b

Please sign in to comment.