在 Joomla 中设置列​​宽的基本 php

2022-01-06 00:00:00 php joomla joomla-template joomla3.1

我正在使用 Joomla 和引导程序来创建 Joomla 模板.我有一个 3 列布局(容器总共 12 个所以 Bootstrap:span12).我在 Joomla 后端设置参数以设置 $left 和 $right 列宽,然后在我的模板中回显这些变量以设置左右 div 列宽(在 index.php 中)

但是,我想在我的模板中使用一些简单的逻辑,根据后端参数中输入的 $left 和 $right 值自动计算和设置中间列的跨度值.

我几乎对 PHP 的了解几乎为零,所以请原谅我这段代码的粗糙.我只是想检查我在做什么是正确的还是愚蠢的,或者是否有更好的方法.这就是我在做什么...

params->get('sidebarLeftWidth', '');$right = $this->params->get('sidebarRightWidth', '');$网格= 12;$span = $grid - ( $left + $right );?>

然后在我的 html 中设置中间列的宽度 - 简单地...

class="span<?php echo $span; ?>"

解决方案

好吧,如果您正确设置了参数,它看起来应该可以工作.

您实际上并没有说明发生了什么问题,即正在生成什么结果,因此很难准确判断,因此这里有一些背景信息和建议可以帮助您弄清楚.对于模板开发,您可以在 Joomla Docs 网站上找到更多信息.joomla.org/Template_Development" rel="nofollow">模板开发.

假设你的代码在你的模板中 index.php:

  1. $this->params->get('sidebarLeftWidth','') 正在获取一个名为 sidebarLeftWidth 的模板参数,但如果该参数不可用,然后将其设置为 '' 有效 null.

  2. get 中的参数名称应使用与 templateDetails.xml 文件中完全相同的拼写和大小写来定义.如果不是,您的 $left$right 可能为空.(编辑问题以包含模板 XML 或其中的一部分会有所帮助.)

  3. params 部分是一个 JRegistryobject 并根据最初存储在对象的 name 属性中的内容返回混合类型(通常这是一个字符串,但它可以是 PHP 可以处理的任何内容).要强制输入值,您可能需要更改 get 行以将结果转换为整数并在未找到任何内容时返回 0:

    $left = (int) $this->params->get('sidebarLeftWidth', 0);

    $right = (int) $this->params->get('sidebarRightWidth', 0);

  4. 在调试器中检查 params 的内容,即检查 $this->params 对象中每个命名参数的值.如果您不使用 IDE,请尝试执行 print_r():

    echo '

    '.print_r($this->params, true) .'</pre>';

I am using Joomla with bootstrap to create a Joomla template. I have a 3 column layout (container totalling 12 so Bootstrap: span12). I am setting parameters in the Joomla backend to set the $left and $right column widths and then echoing those variables in my template to set the left and right div column widths (in index.php)

However, I want to use a simple bit of logic in my template to automatically calculate and set the span value of the middle column based on the $left and $right values entered in the parameters in the backend.

I literally know almost zero PHP so please forgive me for the crudeness of this code. I just want to check if what I am doing is correct or plain stupid or if there is a better way. Here's what I am doing...

<?php
$left = $this->params->get('sidebarLeftWidth', '');
$right = $this->params->get('sidebarRightWidth', '');

$grid = 12;
$span = $grid - ( $left + $right );
?>

and then to set the width of my middle column in my html - simply...

class="span<?php echo $span; ?>"

解决方案

Well, that looks like it should work, if you have set the parameters correctly.

You don't actually say what's going wrong, i.e. what result is being generated so it's a bit hard to tell exactly, so here's some background info and suggestions that may help you figure it out. For template development you can find more at the Joomla Docs website on Template Development.

Assuming your code is in your templates index.php:

  1. $this->params->get('sidebarLeftWidth','') is getting a template parameter called sidebarLeftWidth, but if that parameter isn't available then it's setting it to '' effectively null.

  2. The parameter names in your get should be defined with exactly the same spelling and capitalisation as in your templateDetails.xml file. If not your $left and $right may be empty. (It will help if you edit your question to include the template XML, or part of it.)

  3. The params part is a JRegistry object and returns a mixed type depending on what is originally stored in the name attribute of the object (usually this is a string, but it could be anything PHP can handle). To force an in value you may want to change your get lines to cast the results as integers and return 0 if nothing is found:

    $left = (int) $this->params->get('sidebarLeftWidth', 0);

    $right = (int) $this->params->get('sidebarRightWidth', 0);

  4. Check the contents of your params in your debugger, i.e. check the values of each of your named parameters in the $this->params object. If you're not using an IDE try doing a print_r():

    echo '<pre>' . print_r($this->params, true) . '</pre>';

相关文章