duannuan0074 2014-08-16 13:35
浏览 25

用于多个站点的HTML模板

I'm preparing an application that will serve several different sites that have a common admin panel (a page with different themes to simplify).

Each of these "themes" will have different needs. For example while all of them show a list of services, some of them will show an associated image too. For the ones who don't I would prefer to avoid the DB calls to fetch them (different logic to render the pages).

In Laravel (a PHP framework) this would be a perfect use for view composers.

What would be the design of such a system in go?

I was thinking in some kind of "hooks" that each theme can register to run functions to fetch and add data for a specific template. There's a better way to do it?

  • 写回答

1条回答 默认 最新

  • duandunzhuo3234 2014-08-16 14:02
    关注

    If you pass a list of service objects to the template, you could easily decide inside the template what you want to show. If you decide to show a service's image, you could call the ServiceObject.ImageURL() (which would then lead to a database call). If you're not calling this function, there will be no database query issued.

    A sample using the pongo2 template engine:

    {% for service in services %}
        {% if page.theme == "simple" %}
            <p>We don't show any image in the simple theme.</p>
        {% else %}
            {# This would lead to a database call and return the image path #}
            <img src="{{ service.ImageURL() }}" />
        {% endif %}
    {% endfor %}
    

    Another idea would be to simply create an individual template per theme (extending the common base template); you don't have check for the theme inside the template then.

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能