MDL-57972 mustache: add shortentext template helper
Part of MDL-55611
This commit is contained in:
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -29,14 +29,14 @@ define(['core/mustache',
|
||||
'core/str',
|
||||
'core/notification',
|
||||
'core/url',
|
||||
'core/log',
|
||||
'core/config',
|
||||
'core/localstorage',
|
||||
'core/event',
|
||||
'core/yui',
|
||||
'core/log'
|
||||
'core/log',
|
||||
'core/truncate'
|
||||
],
|
||||
function(mustache, $, ajax, str, notification, coreurl, log, config, storage, event, Y, Log) {
|
||||
function(mustache, $, ajax, str, notification, coreurl, config, storage, event, Y, Log, Truncate) {
|
||||
|
||||
// Module variables.
|
||||
/** @var {Number} uniqInstances Count of times this constructor has been called. */
|
||||
@@ -261,6 +261,33 @@ define(['core/mustache',
|
||||
return '"' + content + '"';
|
||||
};
|
||||
|
||||
/**
|
||||
* Shorten text helper to truncate text and append a trailing ellipsis.
|
||||
*
|
||||
* @method shortenTextHelper
|
||||
* @private
|
||||
* @param {object} context The current mustache context.
|
||||
* @param {string} sectionText The text to parse the arguments from.
|
||||
* @param {function} helper Used to render subsections of the text.
|
||||
* @return {string}
|
||||
*/
|
||||
Renderer.prototype.shortenTextHelper = function(context, sectionText, helper) {
|
||||
// Non-greedy split on comma to grab section text into the length and
|
||||
// text parts.
|
||||
var regex = /(.*?),(.*)/;
|
||||
var parts = sectionText.match(regex);
|
||||
// The length is the part matched in the first set of parethesis.
|
||||
var length = parts[1].trim();
|
||||
// The length is the part matched in the second set of parethesis.
|
||||
var text = parts[2].trim();
|
||||
var content = helper(text, context);
|
||||
return Truncate.truncate(content, {
|
||||
length: length,
|
||||
words: true,
|
||||
ellipsis: '...'
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Add some common helper functions to all context objects passed to templates.
|
||||
* These helpers match exactly the helpers available in php.
|
||||
@@ -287,6 +314,9 @@ define(['core/mustache',
|
||||
context.quote = function() {
|
||||
return this.quoteHelper.bind(this, context);
|
||||
}.bind(this);
|
||||
context.shortentext = function() {
|
||||
return this.shortenTextHelper.bind(this, context);
|
||||
}.bind(this);
|
||||
context.globals = {config: config};
|
||||
context.currentTheme = themeName;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Mustache helper shorten text.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2017 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use Mustache_LambdaHelper;
|
||||
use renderer_base;
|
||||
|
||||
/**
|
||||
* This class will call shorten_text with the section content.
|
||||
*
|
||||
* @copyright 2017 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mustache_shorten_text_helper {
|
||||
|
||||
/**
|
||||
* Read a length and text component from the string.
|
||||
*
|
||||
* {{#shortentext}}50,Some test to shorten{{/shortentext}}
|
||||
*
|
||||
* Both args are required. The length must come first.
|
||||
*
|
||||
* @param string $args The text to parse for arguments.
|
||||
* @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
|
||||
* @return string
|
||||
*/
|
||||
public function shorten($args, Mustache_LambdaHelper $helper) {
|
||||
// Split the text into an array of variables.
|
||||
list($length, $text) = explode(',', $args, 2);
|
||||
$length = trim($length);
|
||||
$text = trim($text);
|
||||
|
||||
// Allow mustache tags in the text.
|
||||
$text = $helper->render($text);
|
||||
|
||||
return shorten_text($text, $length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,7 @@ class renderer_base {
|
||||
$quotehelper = new \core\output\mustache_quote_helper();
|
||||
$jshelper = new \core\output\mustache_javascript_helper($this->page->requires);
|
||||
$pixhelper = new \core\output\mustache_pix_helper($this);
|
||||
$shortentexthelper = new \core\output\mustache_shorten_text_helper();
|
||||
|
||||
// We only expose the variables that are exposed to JS templates.
|
||||
$safeconfig = $this->page->requires->get_config_for_javascript($this->page, $this);
|
||||
@@ -99,7 +100,8 @@ class renderer_base {
|
||||
'str' => array($stringhelper, 'str'),
|
||||
'quote' => array($quotehelper, 'quote'),
|
||||
'js' => array($jshelper, 'help'),
|
||||
'pix' => array($pixhelper, 'pix'));
|
||||
'pix' => array($pixhelper, 'pix'),
|
||||
'shortentext' => array($shortentexthelper, 'shorten'));
|
||||
|
||||
$this->mustache = new Mustache_Engine(array(
|
||||
'cache' => $cachedir,
|
||||
|
||||
Reference in New Issue
Block a user