在动作助手中,我可以使用 $this->getRequest();
有什么类似的视图助手吗?
我正在制作一个电子商务应用程序,其中的类别在所有页面的侧栏中都可见.我在应用程序控制器中写了一个方法
def categories
@categories = Category.all
end
Run Code Online (Sandbox Code Playgroud)
但是我怎么能默认这个方法可用于所有控制器和动作,这样我就不必在每个动作中专门调用这个方法
def list
categories
@products = Product.order('title').page(params[:page]).per(4)
end
Run Code Online (Sandbox Code Playgroud) 我有太多的文字实用的方法,如MakeShortText(string text, int length),RemoveTags(string text),TimeAgo(DateTime date)等.我想从单独的帮助器访问它们,如下例所示:
@Text().MakeShortText(Model.Text, 10)
Run Code Online (Sandbox Code Playgroud)
是否可以创建此类扩展?或者我必须像这样为HtmlHelper做扩展:
@Html.Text().MaksShortText(Model.Text, 10)
Run Code Online (Sandbox Code Playgroud)
?
我正在使用Zend Framework 1.8.我有一个问题,headMeta()复制我的元关键字.
在我的layout.phtml中,我有
<?php echo $this->headMeta(); ?>
我有一个自定义Controller_Plugin_ViewSetup(扩展Zend_Controller_Plugin_Abstract),其中包含以下代码,在dispatchLoopStartup()函数中:
$view->headMeta()->setHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$view->headMeta()->setName('keywords', 'global,generic,keywords,');
最后,在我的视图脚本中,我有以下内容:
$this->headMeta()->appendName('keywords', 'view,specific,keywords');
我期待在我的HTML源代码中,我会看到:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="keywords" content="global,generic,keywords,view,specific,keywords" />
但是,我实际上看到了这个:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="keywords" content="global,generic,keywords," />
<meta name="keywords" content="view,specific,keywords" />
换句话说,元关键字并不像它们应该那样连接在一起.我究竟做错了什么?
干杯,
马特
在Rails 3中,我使用以下帮助程序来获得奇怪的颜色表:
def bicolor_table(collection, classes = [], &block)
string = ""
even = 0
for item in collection
string << content_tag(:tr, :class => (((even % 2 == 0) ? "even " : "odd ") + classes.join(" "))) do
yield(item)
end
even = 1 - even
end
return string
end
Run Code Online (Sandbox Code Playgroud)
我在我的观点中使用它:
<%= bicolor_table(services) do |service| %>
<td><%= image_tag service.area.small_image %></td>
<td><%= link_to service.title, service %></td>
<% end %>
Run Code Online (Sandbox Code Playgroud)
现在,我必须将应用程序迁移到Rails 2.问题是Rails 2不使用Erubis,因此当它找到<%= whatever%>标记时,它只调用whatever.to_s.所以,在我的情况下,这导致尝试执行
(bicolor_table(services) do |service|).to_s
Run Code Online (Sandbox Code Playgroud)
带来明显(坏)的后果.问题是:我原则上是错的(比如"帮助者不应该这样工作,而是使用......")或者我应该寻找解决方法吗?
谢谢.
<%= f.check_box :openid_enabled %>
<%= f.label :openid_enabled, 'OpenID' %>
Run Code Online (Sandbox Code Playgroud)
上面的代码生成了这个HTML
<input type="hidden" value="0" name="application[openid_enabled]">
<input type="checkbox" value="1" name="application[openid_enabled]" id="application_openid_enabled">
<label for="application_openid_enabled">OpenID</label>
Run Code Online (Sandbox Code Playgroud)
并且标签显示为
[x]
OpenID
Run Code Online (Sandbox Code Playgroud)
代替
[x] OpenID
Run Code Online (Sandbox Code Playgroud)
我是否需要设计样式或者rails帮助程序是否具有一些内置功能?
添加
我在我的Rails应用程序中使用twitter bootstrap CSS框架.
我写了一个表单视图助手,它扩展Zend\Form\View\Helper\FormMultiCheckbox并覆盖了它的renderOptions(...)方法:
<?php
namespace MyNamespace\Form\View\Helper;
use Zend\Form\View\Helper\FormMultiCheckbox as ZendFormMultiCheckbox;
class FormMultiCheckbox extends ZendFormMultiCheckbox
{
protected function renderOptions(...)
{
...
$label = $escapeHtmlHelper($label);
$labelOpen = $labelHelper->openTag($labelAttributes);
switch ($labelPosition) {
case self::LABEL_PREPEND:
$template = $labelOpen . $label . $labelClose . '%s';
break;
case self::LABEL_APPEND:
default:
$template = '%s' . $labelOpen . $label . $labelClose;
break;
}
$markup = sprintf($template, $input);
$combinedMarkup[] = $markup;
...
}
}
Run Code Online (Sandbox Code Playgroud)
下一步是注册新的视图助手.我这样做就像这里所示:
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module {
... …Run Code Online (Sandbox Code Playgroud) 我有一个步骤定义:
Then(/^I should see the total price of the products$/) do
# assumes all existing products are in the cart
total = Product.find(:all).inject { |sum, p| sum + p.price }
page.find(".total").should have_content number_to_currency(total)
end
Run Code Online (Sandbox Code Playgroud)
这爆发了 undefined method 'number_to_currency' for #<Cucumber::Rails::World:0xbef8d18> (NoMethodError)
我可以通过模拟number_to_currency帮助程序的行为来模拟"total" ,但我宁愿重用Rails中的帮助程序,因为在这一步中我对格式化不感兴趣,只是计算和渲染总数.
如何在Capybara中包含NumberHelper或任何视图助手,以便从步骤定义进行访问?
问题:我写了一个条件VH(扩展AbstractConditionViewHelper)并且它通常工作,无论如何我意识到在非缓存版本中它只被评估一次.最初,我认为这是我的错误,但检查常见<f:if> ,问题是相同的:S
通常,当我第一次访问我的页面时,评估条件并给出有效结果,但是当我刷新页面时,不再调用VH(通过在VH内设置断点来检查),并且VH始终被视为假.只有视图代码的任何更改都会导致VH被评估一次,并且下一次刷新将不再调用VH.
typo3conf/EXT /工具箱/班/ ViewHelpers/IsFieldRequiredViewHelper.php:
<?php
namespace Vendor\Toolbox\ViewHelpers;
class IsFieldRequiredViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper {
/**
* @param string $fieldName Current field name
* @param string $requiredFields List of required names separated by commas
*
* @return string the rendered string
*/
public function render($fieldName, $requiredFields) {
$requiredArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $requiredFields, true);
return (in_array($fieldName, $requiredArray))
? $this->renderThenChild()
: $this->renderElseChild();
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
{namespace toolbox=Vendor\Toolbox\ViewHelpers}
<toolbox:isFieldRequired fieldName="foo" requiredFields="foo, bar, baz">
<f:then>TRUE</f:then>
<f:else>FALSE</f:else>
</toolbox:isFieldRequired>
Run Code Online (Sandbox Code Playgroud)
对于我的第一次打击,TRUE但后来才有 …
我想做一个简单的比较,例如相当于:
if ($somevar === 'somestring')
现在,我找到了一些这样做的例子:
<f:if condition="{somevar} == 'somestring'">
...
Run Code Online (Sandbox Code Playgroud)
TYPO3 core 9.5 中也有很多地方,例如.
但官方文档告诉我们不然,我们必须使用基于数组的奇怪的解决方法:
不允许使用 XX/YY 处的字符串,但暂时可以通过比较数组来实现字符串比较
由于实现复杂,Fluid 还无法与字符串进行比较,例如 ....。
(外部基础/流体)
不管什么意思 ...
为了避免问为什么:在 TYPO3 9 中比较字符串和变量的推荐方法是什么?从什么时候可以做到这一点?
view-helpers ×10
fluid ×2
php ×2
typo3 ×2
asp.net-mvc ×1
c# ×1
capybara ×1
css ×1
forms ×1
global ×1
helper ×1
razor ×1
typo3-7.6.x ×1
typo3-9.x ×1
viewhelper ×1