我可以在没有jQuery的情况下访问数据属性吗?
使用jQuery很容易,但是如果没有jQuery,我无法在任何地方看到如何做到这一点.
如果我在没有jQuery的情况下搜索Google,我得到的就是jQuery示例.
它甚至可能吗?
我这样做很好用:
parameters:
array_name1: [a, b, c, d]
array_name2: [x, y, a, b]
Run Code Online (Sandbox Code Playgroud)
现在我需要在PHP中添加$ array_name3 [1] = array("a","b","c")等等,这样的事情:
parameters:
array_name3[1]: [1, 2, 3]
array_name3[2]: [a, b, c]
array_name3[3]: [x, y, z]
Run Code Online (Sandbox Code Playgroud)
......当然不起作用.我没有尝试任何东西似乎被接受.
如何在Yaml(Symfony2)中定义多维数组?
我知道我可以这样做:
app.request.get('name')
Run Code Online (Sandbox Code Playgroud)
......但我该怎么做?
app.request.get(twig_var_name)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了所有明显的解决方案{{}}()等.
可能吗?
我正在尝试操纵URL中的查询字符串值.
我可以从Request对象或Twig的函数中获取当前的URL或路由,但它是我正在努力的查询字符串.
我不需要app.request.attributes.get('_ route_params'),因为这会获取路径中的查询字符串参数.
我需要获取URL中实际存在的查询字符串参数.
我希望能够在Symfony2(在PHP控制器中)和Twig(在Twig模板中)中执行下面列出的两项操作:
获取URL中的所有当前查询字符串值并显示它们
执行1,但在显示之前更改其中一个查询字符串值
我找不到任何知道怎么做的人.
我创建了一个Twig扩展:
{{ image ("image.png", 200) }}
Run Code Online (Sandbox Code Playgroud)
我知道我可以做
{{ image ("image.png", 200)|raw }}
Run Code Online (Sandbox Code Playgroud)
...但我更喜欢使用PHP代码,以便所有内容(来自此'图像'扩展名)都不会被转义.
我看不出这可能.
我知道我可以防止在Twig中所有输出转义,但我只想让这一个扩展不要逃避输出,其他一切都要这样做.
我真的很难看到如何做到这一点.我想检查一个类是否存在于元素的一个父元素中的某个地方.
我不想使用任何库,只需使用vanilla JS.
在下面的示例中,如果有问题的元素位于元素的子节点中,并且"the-class"作为类名,则它应返回true.
我认为jQuery会是这样的:
if( $('#the-element').parents().hasClass('the-class') ) {
return true;
}
Run Code Online (Sandbox Code Playgroud)
所以这返回true:
<div>
<div class="the-class">
<div id="the-element"></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这样做:
<div class="the-class">
<div>
<div id="the-element"></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
...但是这会返回false:
<div>
<div class="the-class">
</div>
<div id="the-element"></div>
</div>
Run Code Online (Sandbox Code Playgroud) 如何访问Twig Extension中的Request对象?
namespace Acme\Bundle\Twig;
use Twig_SimpleFunction;
class MyClass extends \Twig_Extension
{
public function getFunctions()
{
return array(
new Twig_SimpleFunction('xyz', function($param) {
/// here
$request = $this->getRequestObject();
})
);
}
public function getName() {
return "xyz";
}
}
Run Code Online (Sandbox Code Playgroud) 在PHP中我会这样做:
foreach( $array as $key => $value ) {
echo $another_array[$key];
}
Run Code Online (Sandbox Code Playgroud)
我无法在Twig中看到如何做到这一点(在Symfony2中).我尝试了各种各样的东西,但这似乎是明显的答案,但它不起作用.它返回一个'Item"the_index"for"Array"在'错误中不存在'.
{% for value in array %}
{% set the_index = loop.index %}
{{ another_array.the_index }}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我正在使用这个:
$this->getDoctrine()->getRepository('MyBundle:MyEntity')->findAll(array(), Query::HYDRATE_ARRAY);
Run Code Online (Sandbox Code Playgroud)
我认为应该确保它返回一个数组的数组,但它仍然返回一个对象数组.
我需要将整个结果作为数组的数组返回,这样我才能做到这一点(愚蠢的例子,但它解释了我的意思):
<?php
$result = $this->getDoctrine()->getRepository('MyBundle:MyEntity')->findAll('return-an-array');
?>
This is the age of the person at the 5th record: <?php echo $result[4]['age']; ?>
Run Code Online (Sandbox Code Playgroud) 我真的很难理解这一点,现在我只是围成一圈.
我已经阅读了尽可能多的手册,付了一个视频教程,搜索谷歌和YouTube,但是无法让这个工作.
我只是想设置一个在每个请求之前激活的监听器.我可以这样做,但我的问题是可以访问我需要的各种其他部分.
下面是一个例子,但我认为只有实际的代码才能让我理解这一点.
如果有人能填补空白,我将不胜感激.这只是一个例子,但每个部分都会向我解释我需要知道的是什么.
在config.yml中:
services:
kernel.listener.request_listener:
class: Acme\Bundle\NewBundle\EventListener\RequestListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
arguments: [ '@service_container' ]
Run Code Online (Sandbox Code Playgroud)
班级:
namespace Acme\Bundle\NewBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
***do I need to 'use' any others here?***
class RequestListener
{
public function onKernelRequest($container) {
//reference to these: http://api.symfony.com/2.1/Symfony/Component/HttpKernel/Event/KernelEvent.html
$kernel =
//reference to the Request object
$request = $kernel->getRequest();
//reference to the Response object
$response =
//options:
// (1) continue to run usual content
// (2) stop execution and output a …Run Code Online (Sandbox Code Playgroud)