Skip to content

Commit

Permalink
Merge pull request #106 from glogiotatidis/fixsnippet_id
Browse files Browse the repository at this point in the history
Fix snippet_id in snippet edit and document.
  • Loading branch information
glogiotatidis committed Apr 17, 2015
2 parents eff2999 + 570e050 commit 88f9b78
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
24 changes: 24 additions & 0 deletions docs/developing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,30 @@ Snippet requirements
a snippet. Snippets over 500 kilobytes large must be cleared with the
development team first.

Helpers
-------

Accessing snippet id
^^^^^^^^^^^^^^^^^^^^
To get the snippet id within a snippet template use `snippet_id` Jinja2 variable like this:

.. code-block:: html

<div class="snippet">
This is snippet id {{ snippet_id }}.
</div>

The syntax in a snippet is slightly different and uses square brackets `[[snippet_id]]`. Here is an example that uses the `Raw Template`:

.. code-block:: html

<div class="snippet">
This is snippet id [[snippet_id]].
</div>

.. warning:: Beware that in this case spacing matters and `[[ snippet_id ]]` will not work.


.. _testing:

Testing
Expand Down
2 changes: 1 addition & 1 deletion snippets/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def render(self):
# Add snippet ID to template variables.
for key, value in data.items():
if isinstance(value, basestring):
data[key] = value.replace(u'<snippet_id>', unicode(snippet_id))
data[key] = value.replace(u'[[snippet_id]]', unicode(snippet_id))

# Use a list for attrs to make the output order predictable.
attrs = [('data-snippet-id', self.id),
Expand Down
4 changes: 2 additions & 2 deletions snippets/base/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ def test_render_no_snippet_id(self):
def test_render_data_with_snippet_id(self):
"""
Any strings included in the template context should have the
substring "<snippet_id>" replaced with the ID of the snippet.
substring "[[snippet_id]]" replaced with the ID of the snippet.
"""
snippet = SnippetFactory.build(template__code='<p>{{ code }}</p>',
data='{"code": "snippet id <snippet_id>", "foo": true}')
data='{"code": "snippet id [[snippet_id]]", "foo": true}')
snippet.template.render = Mock()
snippet.render()
snippet.template.render.assert_called_with({'code': 'snippet id 0', 'snippet_id': 0,
Expand Down

0 comments on commit 88f9b78

Please sign in to comment.