def get_html(cls): if cls.entry_point_path is None: raise Exception( 'No entry-point specified for skin %s.' % cls.skin_id) return jinja2.Markup(jinja_utils.get_jinja_env( feconf.SKINS_TEMPLATES_DIR).get_template('%s/%s' % ( cls.skin_id, cls.entry_point_path)).render())
def jinja2_env(self): """Returns a Jinja2 environment cached for frontend templates. Returns: Environment. A Jinja2 environment object used to load templates. """ return jinja_utils.get_jinja_env(feconf.FRONTEND_TEMPLATES_DIR)
def get_html(cls): if cls.entry_point_path is None: raise Exception('No entry-point specified for skin %s.' % cls.skin_id) return jinja2.Markup( jinja_utils.get_jinja_env(feconf.SKINS_TEMPLATES_DIR).get_template( '%s/%s' % (cls.skin_id, cls.entry_point_path)).render())
def test_get_jinja_env(self): env = jinja_utils.get_jinja_env(feconf.FRONTEND_TEMPLATES_DIR) self.assertTrue(env.autoescape) self.assertEqual(env.loader.searchpath, [ os.path.join(os.path.dirname(__file__), feconf.FRONTEND_TEMPLATES_DIR) ]) for jinja_filter in jinja_utils.JINJA_FILTERS: self.assertTrue(jinja_filter in env.filters)
def render_view(cls, data): """Renders a view-only version of the Object, suitable for editors of it. The default implementation uses takes the Jinja template supplied in the class and renders against it. This template has autoescape=True turned on, so strings that are passed in will be escaped. The default implementation can be overwritten by subclasses -- but they are then responsible for ensuring that autoescaping takes place. Args: data: the normalized Python representation of the Object. """ assert cls.view_html_filename, 'No view template specified.' return jinja_utils.get_jinja_env( feconf.OBJECT_TEMPLATES_DIR).get_template( '%s.html' % cls.view_html_filename).render({'data': data})
def jinja2_env(self): return jinja_utils.get_jinja_env(feconf.FRONTEND_TEMPLATES_DIR)
def export_content_to_html(content_array, block_number, params=None, escape_text_strings=True): """Takes a Content array and transforms it into HTML. Args: content_array: an array, each of whose members is of type Content. This object has two keys: type and value. The 'type' is one of the following: - 'text'; then the value is a text string - 'image'; then the value is an image ID - 'video'; then the value is a video ID - 'widget'; then the value is a JSON-encoded dict with keys 'id' and 'params', from which the raw widget HTML can be constructed block_number: the number of content blocks preceding this one. params: any parameters used for templatizing text strings. escape_text_strings: True if values supplied with content of type 'text' should be escaped after Jinja evaluation; False otherwise. Returns: the HTML string representing the array. Raises: InvalidInputException: if content has no 'type' attribute, or an invalid 'type' attribute. """ if params is None: params = {} JINJA_ENV = jinja_utils.get_jinja_env(feconf.FRONTEND_TEMPLATES_DIR) html, widget_array = '', [] for content in content_array: if content.type in ['text', 'image', 'video']: value = (jinja_utils.parse_string(content.value, params) if content.type == 'text' else content.value) if escape_text_strings: value = cgi.escape(value) html += JINJA_ENV.get_template('reader/content.html').render({ 'type': content.type, 'value': value, }) elif content.type == 'widget': # Ignore empty widget specifications. if not content.value: continue widget_dict = json.loads(content.value) widget = widget_domain.Registry.get_widget_by_id( feconf.NONINTERACTIVE_PREFIX, widget_dict['id']) widget_array_len = len(widget_array) html += JINJA_ENV.get_template('reader/content.html').render({ 'blockIndex': block_number, 'index': widget_array_len, 'type': content.type, }) widget_array.append({ 'blockIndex': block_number, 'index': widget_array_len, 'raw': widget.get_raw_code( widget_dict['customization_args'], params), }) else: raise utils.InvalidInputException( 'Invalid content type %s', content.type) return html, widget_array
def get_skin_html(skin_name): """Returns the HTML for a given skin.""" return jinja2.Markup( jinja_utils.get_jinja_env(feconf.SKINS_TEMPLATES_DIR).get_template( '%s.html' % skin_name).render())