草庐IT

templates - tmpl.Execute 和子文件 golang

coder 2023-06-28 原文

我需要帮助。 我需要在子文件("article.html",我的文本中的示例):

// ...
type Page struct {
    Test string
}

type News struct {
    Page
    Title string
}

func main() {
    t, _ := template.ParseFiles(filepath+"core.tmpl", filepath+"article.tmpl")
    p := &News{
        Title: "TITLE",
        Page: Page{
            Test: "TITLE",
        },
    }
    t.Execute(wr, p)
}

core.tmpl中的代码:

{{template "article"}}

article.tmpl中的代码:

{{define "article"}}
{{.Title}}<br><br>
{{.Page.Test}}
{{end}}

最佳答案

在你的 core.tmpl 你必须使用

{{template "article" .}}

如果最后没有指定 .,模板将以 nil 数据执行。指定 . 会将 . 的值传递给调用的模板。

引自text/template包文档,Actions 部分:

{{template "name"}}
    The template with the specified name is executed with nil data.

{{template "name" pipeline}}
    The template with the specified name is executed with dot set
    to the value of the pipeline.

关于templates - tmpl.Execute 和子文件 golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28874194/

有关templates - tmpl.Execute 和子文件 golang的更多相关文章

  1. ruby-on-rails - 子域 + ActionView::Template::Error(缺少要链接的主机!) - 2

    我已经解决了标题中描述的错误的众多解决方案。ActionView::Template::Error(缺少要链接的主机!请提供:host参数,设置default_url_options[:host],或将:only_path设置为true):但是,该项目还修改了url_for函数以使用子域,如本railscast所示:http://railscasts.com/episodes/221-subdomains-in-rails-3因此,这个错误的传统答案,例如在我的环境设置中设置变量似乎不是解决方案。这里有一些其他的提示:这是一个全新的设置,我刚刚克隆了一个项目并安装了ruby​​、rai

  2. ruby-on-rails - Rails 和 I18n : localized templates vs localized string - 2

    您可能知道,从Rails2.2开始,Rails附带了一个简单的本地化和国际化后端。默认情况下,您可以将需要翻译的字符串存储在config文件夹中的本地化文件中。config/locales/en.ymlconfig/locales/it.yml但是Rails也提供了本地化模板和局部的能力。例如,MainController#index操作可以根据模板文件名和当前区域设置选择本地化模板。apps/views/main/index.it.html.erbapps/views/main/index.en.html.erb当您需要翻译单个字符串或短段落时,第一个功能很有用。当同一Action根

  3. ruby-on-rails - 目录 : template missing error while rendering JSON - 2

    我的Controller中有以下代码:deftljson(result=[])@stat_id=params[:stat_id]@rpm=FedoraRpm.find_by_name(@stat_id)@rpm.ruby_gem.historical_gems.each{|h|resulth.version,:start=>h.build_date}}@rpm.bugs.each{|b|resultb.name+"ViewonBugZilla",:start=>b.bz_id}}@res=result.to_jsonrespond_todo|format|format.json{ren

  4. ruby-on-rails - rails : calling yield in a partial template? - 2

    我有时会在部分erb模板中看到这个:其他时候根本没有yield。在局部调用yield有什么好处? 最佳答案 我过去曾使用过它,如果我有一个可以从不同页面调用的部分,可能需要页面中的一些上下文内容。我的一个用例是菜单。我有我的库存菜单项,但后来我有一个yield(:menu),这样用户加载管理页面的内容,我就可以从页面添加菜单项,而不是在其中编写条件语句部分本身。这是一些伪代码:_menu.haml%ul%liHome%liUsers%liRoles=yield(:menu)用户.haml-content_for:menudo%liA

  5. ruby-on-rails - ActionView::Template::Error(application.css 未预编译): - 2

    我以前在heroku上开发过几个应用程序,从来没有遇到过这个错误:ActionView::Template::Error(application.css未预编译):我最新的推送甚至没有对application.css文件进行更改。这是怎么回事?我看到文章说将production.rb中的一行更改为:#Don'tfallbacktoassetspipelineifaprecompiledassetismissedconfig.assets.compile=true但这不会减慢一切吗?为什么会突然出现这个错误?修复它的最佳方法是什么?编辑:我应该注意到它在开发中工作得很好。

  6. ruby-on-rails - RSpec Controller 测试 : missing template on create - 2

    我有一个有趣的情况。我正在测试以下简单的创建操作:#willonlybeaccessedviaAjaxdefcreateclick=Click.new(params[:click])click.save#don'treallycarewhetheritssuccessorfailureend然后我有以下非常简单的Controller规范:require'spec_helper'describeClicksController,"creatingaclick"doit"shouldcreateaclickforevent"doxhr:post,:create,:click=>{:even

  7. ruby - 在 Windows 8 : Unable to execute file C:\Program Files(x86)\Heroku\ruby-1. 9.3\bin\gem.bat 上安装 heroku toolbelt 时出错 - 2

    我想安装herokutoolbelt。我选择完全安装。安装时出现此错误。安装完成后herokulogin不起作用。 最佳答案 试试这个:Followthislink无论它是否与在Windows7上运行游戏有关,请在您的Windows8上尝试这个。如果它有效,请告诉我们。 关于ruby-在Windows8:UnabletoexecutefileC:\ProgramFiles(x86)\Heroku\ruby-1.9.3\bin\gem.bat上安装herokutoolbelt时出错,我们在

  8. ruby - "Execute and Update ' # => ' Markers"对于 VIM - 2

    VIM中是否有可能为ruby​​代码重复“执行和更新‘#=>’标记”TextMate功能。我想要这样的东西:x=2classAdefa42endendx#=>A.new.a#=>输入一些命令...然后得到x=2classAdefa42endendx#=>2A.new.a#=>42这是来自CiaránWalsh’sBlog的对此功能的描述:Anothertooldefinitelyworthknowingisthe"ExecuteandUpdate'#=>'Markers"command(on⌃⇧⌘Ebydefault).Touseit,addsomecommentmarkers(the

  9. sql - 来自 ActiveRecord::Base.connection.execute(sql) - PostgreSQL 的结果 - 2

    在使用ActiveRecord::BaseConnection类执行SQL语句后,如何找到PostgreSQL处理的记录数?temp_sql="UPDATEtable_aSETcolumn_a='abc'WHEREcolumn_b=1"result=ActiveRecord::Base.establish_connection(@db).connection.execute(temp_sql)或者您可以建议更好的方法来做到这一点。请记住,上面的更新声明是一个简单的更新声明,以保持问题简短。我真正的查询是“基于集合”的,涉及复杂的创建临时表、更新、插入语句。

  10. sql - ActionView::Template::Error (PG::UndefinedFunction: ERROR: operator does not exist: integer ~~ 未知 - 2

    为了在Heroku中使用,我从MySQL切换到了PostgreSQL。现在我的搜索不起作用。无法弄清楚运营商出了什么问题。ActionView::Template::Error(PG::UndefinedFunction:ERROR:operatordoesnotexist:integer~~unknown.2014-11-11T19:59:58.082607+00:00app[web.1]:ProcessingbyAllListingsController#search_listingsasJS2014-11-11T19:59:58.105074+00:00app[web.1]:4:

随机推荐