【问题标题】:Is it possible to write a HTML file with inline css by using perl CGI.pm?是否可以使用 perl CGI.pm 编写带有内联 css 的 HTML 文件?
【发布时间】:2013-06-18 07:32:19
【问题描述】:

我被要求使用CGI.pm和来自用户的表单来制作一个获取宽度、单位、方向和段落的表单。然后我需要通过 CSS 规则为段落应用宽度、单位和方向。

我想知道是否可以使用 CGI.pm 在 Perl 中编写内联 CSS。

例如:

<p style=width:"user-width user-unit";text-align:"user-justification">
 paragraph</p> 

【问题讨论】:

  • 最好不要混合 View(HTML,CSS) 和 Controller(你的 CGI 脚本做逻辑)
  • 老师让我练习一下
  • 如果你必须在课堂上这样做,那么你必须在课堂上这样做,我猜……但你正在养成一个坏习惯。如今,使用 CGI.pm 的 HTML 生成函数通常被认为是一个坏主意。改用模板系统(例如 Template::Toolkit 或 Xslate)是正确的做法,至少在出现更好的东西之前是这样。

标签: perl cgi perl-module


【解决方案1】:

是的,可以使用-style 参数。

例如:print h1({-style=&gt;'Color: red;'},'Welcome to Hell');

您还可以创建单独的 CSS 文件。下面是一个使用单独样式表 (style.css) 和内联 CSS 的示例。

print start_html( -title=>'CGI with Style',
                      -style=>{-src=>'http://www.example.com/style/style.css',
                               -code=>$newStyle}
                     );
    print h1('CGI with Style'),
          p({-class=>'Tip'},
            "Better read the cascading style sheet spec before playing with this!"),
          span({-style=>'color: magenta'},
               "Look Mom, no hands!",
               p(),
               "Whooo wee!"
               );
    print end_html;

来源:CGI.pm docs

【讨论】:

  • 如何将用户提供的值关联到 css 属性?
  • 对不起,我没有得到你。举个例子吧。
  • 打印 p({-style=>'width:$cgi->param('width')'});我想使用用户输入来设置css中的宽度
  • 它应该可以正常工作,不是吗?确保您的 $cgi 是 CGI.pm 模块的对象。
猜你喜欢
  • 2011-06-13
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多