Phoenix additional layout variables like @inner

后端 未结 1 1338
予麋鹿
予麋鹿 2020-12-17 16:50

I am looking to add additional layout parameters like the @inner for the layout. For example @title for the @ti <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5408099190056760" data-ad-slot="7305827575" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="fly-panel detail-box" id="flyReply"> <fieldset class="layui-elem-field layui-field-title" style="text-align: center;"> <legend>1条回答</legend> </fieldset> <ul class="jieda" id="jieda"> <li data-id="111" class="jieda-daan"> <a name="item-1111111111"></a> <div class="detail-about detail-about-reply"> <a class="fly-avatar" href=""> <img src="https://www.e-learn.cn/qa/data/avatar/000/00/00/small_000000025.jpg" alt=" 执笔经年 "> </a> <div class="fly-detail-user"> <a href="" class="fly-link"> <cite> 执笔经年</cite> </a> <span>(楼主)</span> </div> <div class="detail-hits"> <span>2020-12-17 17:26</span> </div> </div> <div class="detail-body jieda-body photos"> <p> <p>For the page title, you can simply pass a value through from your controller:</p> <pre><code>def edit(conn, params) do render(conn, "edit.html", page_title: "Edit The Thing") end <head> <title><%= assigns[:page_title] || "Default Title" %>

Note that this uses assigns[:page_title] instead of @page_title or assigns.page_title as they will error if the :page_title key is not present in assigns.


For including templates (your script example) there is render_existing/3 (and the docs for the same function in the latest version of Phoenix).

The documentation gives a similar example to what you requested so I have copied it here for convenience:

Consider the case where the application layout allows views to dynamically render a section of script tags in the head of the document. Some views may wish to inject certain scripts, while others will not.


  <%= render_existing view_module(@conn), "scripts.html", assigns %>

Then the module for the @inner view can decide to provide scripts with either a precompiled template, or by implementing the function directly, ie:

def render("scripts.html", _assigns) do
  "

                                 
              
提交回复
热议问题