Search code examples
djangodjango-templatesdjango-cms

Django-cms Placeholder naming from template variable


I am working on generating a menu which allows the user to upload an image, and text description, to each subsection of the menu. Here is the current code (custom template for displaying a menu):

{% for lowest in lower.children %}
    {% with lowest.get_menu_title as title %}
        <div id="product_box">
            <div id="product_image">
                {% placeholder title %}
            </div>
        </div>
        <div id="product_summary">
            <h3>
                <a href="{{ lowest.attr.redirect_url|default:lowest.get_absolute_url }}">
                                    {{title}}
                </a>
            </h3>
            <p>
                {% placeholder "Text" %}
                <a href="{{ lowest.attr.redirect_url|default:lowest.get_absolute_url }}">Learn More</a>
            </p>
        </div>
    {% endwith %}
{% endfor %}

(Don't worry about the {% placeholder "Text" %} right now, I simply haven't changed that part yet since I can't get the former working). The page renders, but only the only plugins visible are "TITLE" and "Text" (again, no worries about "text").

I haven't found any documentation stating that placeholders can't be named using variables, but so far everything I've tried , from {{title}} to {% placeholder lowest.get_menu_title %} all just seem to interpret those as strings, displaying {{TITLE}} or LOWEST.GET_MENU_TITLE as the placeholder name, respectively.

So, my question is: Can I assign a variable name in the template to a placeholder (so that I can generate unique placeholders in a for loop, based on menu names)?


Solution

  • From looking through the codebase here: https://github.com/divio/django-cms/blob/develop/cms/templatetags/cms_tags.py#L284 it appears that the 'name' argument to the placeholder template tag is always interpreted as a string, so, it cannot currently be a variable assignment.

    Its not completely clear what you intend to do here, but I wonder if you should instead look into using a django model to represent all your various instances, then use a PlaceholderField (http://django-cms.readthedocs.org/en/support-3.0.x/how_to/placeholders.html). You still won't be able to define/reference the placeholders through template variables, but you could then iterate through your instances and display their PlaceholderField in turn.

    Hope this helps.