【问题标题】:Rails 3 templates: rendering multiple formats using the same template handlerRails 3 模板:使用相同的模板处理程序呈现多种格式
【发布时间】:2010-12-29 14:56:55
【问题描述】:

来自包含例如的单个视图文件带有 ERB 插入的 LaTeX 代码,我希望能够:

  1. 通过评估 ERB 渲染到 LaTeX 源文件

  2. 通过使用自定义 latex_to_pdf() 函数编译之前的结果,渲染为 PDF

第一种情况可以通过注册模板处理程序来实现:

ActionView::Template.register_template_handler :latex, LatexSource

其中 LatexSource 是 ActionView::Template::Handler 的子类,实现 call(template) 或 compile(template)。

这允许视图文件“action.tex.latex”作为“controller/action.tex”被正确访问和处理。

不过,第二种情况似乎要困难得多:

  • 如何将请求“controller/action.pdf”像“controller/action.tex”一样发送到模板处理程序,并在发送前通过latex_to_pdf()传递结果回应用户?

非常感谢

【问题讨论】:

    标签: ruby-on-rails-3


    【解决方案1】:

    您不能只注册另一个模板处理程序:pdf,其compile 方法看起来与此类似吗?:

    def compile
       latex_to_pdf LatexSource.compile(template)
    end
    

    更新: 好的,对,这导致需要复制视图(action.tex.latexaction.tex.pdf)。

    下一个想法:

    respond_to do |format| 
      format.latex 
      format.pdf { render :file => latex_to_pdf(render) } 
    end
    

    据我所知,render 在 Rails 2.3 中将渲染的模板作为字符串返回。 我不知道它在 Rails 3 中的表现如何。

    您可以尝试使用render_render_template。如果这可行,我们可以考虑如何让它更干爽舒适,以便进行多项操作。

    【讨论】:

    • 谢谢,但这需要保留两个相同的视图文件副本:“action.tex.latex”和“action.pdf.latex”。我想问题在于试图欺骗模板查找器,即可以从同一个视图文件生成两个不同的输出。
    【解决方案2】:

    我自己(还)没有使用它,但看起来https://github.com/jacott/rails-latex 可以为你完成这项工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-14
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多