ngx_lua arg类型_使用模式,Arg和类型微调Drupal主题

ngx_lua arg类型

In this article, we’ll discuss how you can leverage various Drupal API functions to achieve more fine grained theming. We’ll cover template preprocessing and alter hooks using path patterns, types and args(). We’ll use the arg() function which returns parts of a current Drupal URL path and some pattern matching for instances when you want to match a pattern in a URL. We’ll also take a look at creating a variable for an array of content types.

在本文中,我们将讨论如何利用各种Drupal API函数来实现更细粒度的主题。 我们将介绍模板预处理,并使用路径模式,类型和args()更改钩子。 我们将使用arg()函数,该函数将返回当前Drupal URL路径的一部分,并在您想匹配URL中的模式时返回实例的某些模式匹配。 我们还将看一看为内容类型数组创建变量。

Template preprocessing is a means to define variables for use within your page. For example, you can define a body class variable. In turn you can use the resulting class in your Sass or CSS. An alter or build hook is something that’s run before the page renders so you can do things such as adding JS or CSS to specific pages or content types.

模板预处理是一种定义要在页面中使用的变量的方法。 例如,您可以定义主体类变量。 反过来,您可以在Sass或CSS中使用结果类。 alter或build挂钩是在页面呈现之前运行的,因此您可以执行诸如将JS或CSS添加到特定页面或内容类型的操作。

I’ll explain and demonstrate these hooks and how you can use them to:

我将解释并演示这些挂钩以及如何使用它们来:

  • Add a <body> class to a specific page for better theming

    <body>类添加到特定页面以更好地主题化

  • Add Javascript or CSS to specific pages and paths

    将Javascript或CSS添加到特定的页面和路径
  • Use wildcard path arguments

    使用通配符路径参数
  • URL pattern matching using preg_match

    使用preg_match URL模式匹配

  • Create an array of content types to use as a variable in your argument

    创建内容类型数组以用作参数中的变量
  • Using path arguments as parameters

    使用路径参数作为参数

The functions we discuss here will be added to your theme’s template.php file. Although you can also use these functions in a custom module, you’ll need to specify that the functions are not for admin pages unless that’s your intent, and I’ll cover how to do that.

我们在此讨论的功能将添加到主题的template.php文件中。 尽管您还可以在自定义模块中使用这些功能,但是除非您的意图是,否则您需要指定这些功能不适用于管理页面,而我将介绍如何做到。

入门 (Getting Started)

When using preprocess or alter functions within your theme, you’ll want to be sure template.php exists but if not, you can go ahead and create this file in the root of your theme. So if my theme name is foobar, the path to template.php will be:

在主题中使用预处理或更改功能时,您需要确保template.php存在,但如果不存在,则可以继续在主题的根目录中创建此文件。 因此,如果我的主题名称是foob​​ar ,则template.php的路径将是:

/sites/all/themes/foobar/template.php

API functions are prefaced by the machine name of your theme. For example, if you are using hook_page_alter and your theme name is foobar, we’d write it as function foobar_page_alter(). (The machine name is simply the theme’s folder name.)

API函数以主题的机器名称开头。 例如,如果您使用hook_page_alter且主题名称为foobar,则将其写为function foobar_page_alter() 。 (计算机名称只是主题的文件夹名称。)

使用内容类型数组的自定义主体类 (Custom Body Classes Using a Content Types Array)

A body class is a class that’s added to your HTML <body> tag. For example, on a blog page, you might see something like this:

主体类是添加到HTML <body>标记中的类。 例如,在博客页面上,您可能会看到以下内容:

<body class="page-node page-node-blog">

You can leverage that class in your Sass or CSS to fine tune your theming by doing something like this just for blog pages on your site:

您可以在Sass或CSS中利用该类来对主题进行微调,方法是对网站上的博客页面执行以下操作:

.page-node-blog h1 {
// custom Sass here
}

Out of the box, Drupal 7 comes with some pretty good body classes and usually these are fine for most use cases. In addition, Drupal contribution or Contrib themes such as Zen add enhanced and expanded classes.

开箱即用,Drupal 7附带了一些不错的身体类,通常对于大多数用例来说都很好。 此外,Drupal贡献或Contrib主题(例如Zen)添加了增强和扩展的类。

In our case, we want to add a class to some pages which share some common attributes but may not necessarily derive from the same content type. Let’s say we have two content types that we want to add a specific class to in order to theme those alike but perhaps different from other pages on our website. We can build an array of Drupal content types we want to target and then use that array to add the class. Once we’ve defined the array, we just check to ensure that a given node exists and then pass the array in.

在我们的例子中,我们想向共享某些共同属性但不一定来自同一内容类型的某些页面添加一个类。 假设我们有两种内容类型,我们想要添加一个特定的类,以使它们的主题相同,但可能与我们网站上的其他页面不同。 我们可以构建一个我们想要定位的Drupal内容类型数组,然后使用该数组添加类。 定义数组后,我们只需检查以确保给定节点存在,然后将数组传递进去。

<?php

/**
 * Implements template_preprocess_html().
 *
 * Define custom classes for theming.
 */
function foobar_preprocess_html(&$vars) {

  // Build a node types array from our targeted content types.
  $foo_types = array(
    'landing_page',
    'our_services',
  );

    // Define the node.
    $node = menu_get_object();
  
  // Use the array to add a class to those content types.
  if (!empty($node) && in_array($node->type, $foo_types)) {
    $vars['classes_array'][] = 'page-style-foobar';
    }
  }

This function preprocesses variables for anything that would typically be before the ending HTML </head> tag which is in Drupal’s core template, html.tpl.php. We add the body class using $vars['classes_array']. This variable gets rendered with <?php print $classes; ?> in the <body> tag. In our case, this class will only render in landing_page and our_services content types. Now we can use .page-style-foobar in our Sass or CSS to style these pages.

这个函数对变量进行预处理,该变量通常会在Drupal核心模板html.tpl.php中的 HTML </head>标记之后 。 我们使用$vars['classes_array']添加主体类。 这个变量用<?php print $classes; ?>渲染<?php print $classes; ?> <body>标记中的<?php print $classes; ?> 。 在我们的例子中,这个类将仅呈现LANDING_PAGEour_services内容类型。 现在,我们可以在Sass或CSS中使用.page-style-foobar来设置这些页面的样式。

URL模式匹配 (URL Pattern Matching)

You can also use URL pattern matching for adding useful custom classes. Let’s say we have an “Our Services” landing page and then some sub-pages under that path. The URL architecture might look like this:

您还可以使用URL模式匹配来添加有用的自定义类。 假设我们有一个“我们的服务”登录页面,然后在该路径下有一些子页面。 URL体系结构可能如下所示:

example.com/our-services
- example.com/our-services/subpage-1
- example.com/our-services/subpage-2

We’ll add a custom body class to those pages using preg_match, regex, PHP matches and Drupal’s request_uri function. Once again, we’d put this in a foobar_preprocess_html as above.

我们将使用preg_match ,regex,PHP匹配和Drupal的request_uri函数向这些页面添加一个自定义主体类。 再一次,我们将其放在上面的foobar_preprocess_html中。

<?php
function foobar_preprocess_html(&$vars) {

  // Define the URL path.
  $path = request_uri();

  // Add body classes to various pages for better theming.
  if (preg_match('|^/our-services((?:/[a-zA-Z0-9_\-]*)*)?|', $path, $matches)) {
    $vars['classes_array'][] = 'page-services';
  }
}

Now you can use .page-services .some-common-element for theming these “our-services” pages. Obviously this method has pitfalls if your URL structure changes so it may only fit some use cases.

现在,您可以使用.page-services .some-common-element来设置这些“我们的服务”页面的主题。 显然,如果您的URL结构发生更改,则此方法存在陷阱,因此仅适合某些用例。

路径参数 (Path Arguments)

Another clever way of custom theming specific parts of your site is to partition those off using arg(). Drupal tends to get script and CSS heavy so ideally if you’re adding extra CSS or JS and you don’t need it for every page (which is normally done using your theme’s .info file), you can use path arg() to add these only to pages where they’re needed. For example, if I want to add a custom script to just the Drupal registration page, I can create a path arg() if statement and then add the script within that. The URL path we’ll focus on here is /user/register and you’ll see how the arg() breaks down the URL structure.

自定义主题网站特定部分的另一种巧妙方法是使用arg()将其分区。 Drupal往往会使脚本和CSS变得沉重,因此,理想情况下,如果要添加额外CSS或JS,并且不需要每个页面都使用它(通常使用主题的.info文件完成),则可以使用arg()路径仅将这些添加到需要它们的页面。 例如,如果我只想向Drupal注册页面添加自定义脚本,则可以创建路径arg() if语句 ,然后在其中添加脚本。 我们将在此处重点关注的URL路径是/ user / register ,您将看到arg()如何分解URL结构。

We’ll be using hook_page_alter here which can apply alterations to a page before it’s rendered. First we define the theme path and use Drupal’s attached function.

我们将在此处使用hook_page_alter ,它可以在页面呈现之前对其进行更改。 首先,我们定义主题路径并使用Drupal的附加功能。

<?php

/**
 * Implements hook_page_alter().
 *
 * Add custom functions such as adding js or css.
 */
function foobar_page_alter(&$page, $form) {

  // Define the module path for use below.
  $theme_path = drupal_get_path('theme', 'foobar');

  if (arg(0) == "user" && arg(1) == "register") {
     $foobar_js = array(
      '#attached' => array(
        'js' => array(
          $theme_path . '/js/custom.js' => array(
            'group' => JS_THEME,
          ),
        ),
      ),
    );
    drupal_render($foobar_js);
  
  }
}

In this function, note that we’ve substituted our theme name for hook so hook_page_alter becomes foobar_page_alter. The arg() number signifies the position in the URL path. Zero is first, one is second and so on. You can get pretty creative with these adding more parameters. Let’s say you wanted to add JS to just the user page but no paths underneath it. You could add a NULL arg() after the initial arg().

在此函数中,请注意,我们已将主题名称替换为hook,因此hook_page_alter变为foobar_page_alter 。 arg()数字表示URL路径中的位置。 零是第一,一是第二,依此类推。 通过添加更多参数,您可以变得很有创意。 假设您想将JS仅添加到用户页面,但不添加其下面的路径。 您可以在初始arg()之后添加一个NULL arg()。

if (arg(0) == "user" && arg(1) == NULL) {
// code here
}

In the examples above, we’ve implemented various functions in our theme’s template.php file. You can also use these in a custom module as well and in that case you’d just preface your module name in the function rather than the theme name. When theming from a module, you’ll probably want to exclude admin paths since a module can target anywhere in your site. You can do that excluding admin paths.

在上面的示例中,我们已经在主题的template.php文件中实现了各种功能。 您也可以在自定义模块中使用它们,在这种情况下,您只需在函数中添加模块名称,而不是主题名称即可。 从模块主题化时,您可能要排除管理路径,因为模块可以定位到站点中的任何位置。 您可以执行此操作,但不包括管理路径。

if (!path_is_admin(current_path())) {
// code here
}

结论 (Conclusion)

As you can see, leveraging the Drupal API Toolbox for theming extends your reach as a developer. The examples above are not definitive but they do give you a feel for what’s possible. As a Drupal Themer, using these has helped me expand my bag of tricks when theming and building a site. Comments? Feedback? Leave them below!

如您所见,利用Drupal API工具箱进行主题化可以扩大您作为开发人员的影响力。 上面的示例不是确定性的,但它们确实使您对可能的事情有感觉。 作为Drupal Themer,使用这些内容有助于我在主题化和构建网站时扩展技巧。 注释? 反馈? 把它们留在下面!

资源资源 (Resources)

翻译自: https://www.sitepoint.com/fine-tuning-drupal-themes-patterns-arg-types/

ngx_lua arg类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值