Drupal Views 2:使用自定义标记输出

发布于 2024-10-06 11:18:51 字数 1023 浏览 2 评论 0原文

我有一个简单的视图,可以获取 4 个字段,基本上它可以获取特定内容类型的字段。字段如下:
CSS 类(纯文本)
图片(图片)
标题
身体

很简单的东西。我已经创建了视图,但我需要以专门的方式输出内容,并且我无法确定这些内容如何与我的构建中的自定义标记结合使用。我需要将每一行包装在一个容器中,并且每一行都分解成它自己的容器,请看下面的代码。

   <div id="homepage-folio-portlets">
    <div class="homepage-folio-portlet [CSS class]">
     <div class="homepage-folio-portlet-image"><img src="[Image]" width="450" height="330" alt="" class="[CSS class]-image" /></div>
     <div class="homepage-folio-portlet-text">
      <strong>[Title]</strong>
      <p>[Body]</p>
     </div>
    </div> <!-- /homepage-folio-portlet -->
   </div> <!-- /homepage-folio-portlets -->

所以我有一个容器,homepage-folio-portlet,在其中我想迭代视图,使用类homepage-folio-portlet创建一个新容器对于返回的每一行,包括该行的 CSS 类。

我最大的障碍是弄清楚如何在 template.php 中构建我的 .tpl 文件或我的主题函数。我了解命名约定,但进入后我真的不知道该怎么做。我有一种感觉,无论哪种方式,我都需要在 template.php 中施展一点魔法,以确保我的行输出能够识别内容中的 CSS 类,但谁知道呢。任何帮助和指导表示赞赏。

I have a simple view that grabs 4 fields, basically it grabs the fields of a specific content type. The fields are as follows:
CSS class (plain text)
Image (image)
Title
Body

Pretty simple stuff. I've got the view created, but I need to output things in a specialized way and I can't determine how this stuff breaks down in conjunction with my custom markup from my build. I need to wrap each row in a container and each row breaks down into it's own containers, take a look at the following code.

   <div id="homepage-folio-portlets">
    <div class="homepage-folio-portlet [CSS class]">
     <div class="homepage-folio-portlet-image"><img src="[Image]" width="450" height="330" alt="" class="[CSS class]-image" /></div>
     <div class="homepage-folio-portlet-text">
      <strong>[Title]</strong>
      <p>[Body]</p>
     </div>
    </div> <!-- /homepage-folio-portlet -->
   </div> <!-- /homepage-folio-portlets -->

So I've got a container, homepage-folio-portlets, and inside of that I want to iterate over views creating a new container using the class homepage-folio-portlet for each row returned including the CSS class from the row.

My biggest hurdle is figuring out how to build either my .tpl files or my theme functions in template.php. I understand the naming conventions, but once inside I don't really know what to do. I have a feeling I'll need to do a little magic in template.php either way to make sure that my row output is aware of the CSS class from the content, but who knows. Any help and direction is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小…楫夜泊 2024-10-13 11:18:51

观看上述视频后,我更加清楚如何完成我设定的目标去做。最大的“啊哈”是“行样式输出”模板的默认代码由于 foreach 循环而让我感到困惑。我没有意识到我可以简单地以我认为适合此文件的任何方式输出每个字段,而无需循环。该视频展示了如何使用以下简写 $fields['ID-of-field']->content 单独引用您的字段。要获取“字段 ID”,只需滚动特定视图的“主题信息”选项窗格中的“显示输出”、“样式输出”和“行样式输出”链接即可。

我使用视图编辑屏幕中找到的“主题信息”来确定“行样式输出”的最具体的 .tpl 来创建并创建它,在本例中为 view-view-fields--my-view-name- -default.tpl.php。

view-view-fields--my-view-name--default.tpl.php - 行输出.tpl文件
(不再使用默认的 foreach,因为我知道我想要的字段,而不是循环遍历字段,并且我可以简单地以我认为合适的方式输出它们)

   <div id="homepage-folio-portlets">
    <div class="homepage-folio-portlet <?php print $fields['CSS_class']->content ?>">
     <div class="homepage-folio-portlet-image"><img src="<?php print $fields['Image']->content ?>" width="450" height="330" alt="" class="<?php print $fields['CSS_class']->content ?>-image" /></div>
     <div class="homepage-folio-portlet-text">
      <strong><?php print $fields['Title']->content ?></strong>
      <p><?php print $fields['Body']->content ?></p>
     </div>
    </div> <!-- /homepage-folio-portlet -->
   </div> <!-- /homepage-folio-portlets -->

之后,我对“样式输出”和“显示输出”.tpl 文件,以消除 Drupal 添加的所有额外标记。请注意,我真正关心的是在 .tpl 样式中打印 $row (及其 foreach 循环)和在显示 tpl 中打印 $rows 。它输出的正是我想要的,我高兴极了。终于,它有了一些意义。希望这对其他人有帮助。

仅供参考...

views-view-unformatted--my-view-name--default.tpl.php - 样式 .tpl 文件
(希望将 foreach 循环保留在此处,以便输出每一行)

<?php foreach ($rows as $id => $row): ?>
 <?php print $row; ?>
<?php endforeach; ?>

views-view--my-view-name--default.tpl.php - 显示 .tpl 通过删除所有额外的标记,

<?php print $rows; ?>

我丢失了特定于视图的重要内容,例如管理链接等,但就我的目的而言,这很好。

After watching the aforementioned video it became a little more clear how to accomplish what I set out to do. The biggest "ah-ha" was that the default code for the "Row style output" template was confusing to me because of the foreach loop. I didn't recognize that I could simply output each field in whatever way I see fit in this file without the loop. The video showed how you could reference your fields individually with the following shorthand $fields['ID-of-field']->content. To get the 'ID-of-field' it's as scrolling past the "Display output", "Style output", and "Row style output" links in the "Theming information" option pane of your specific view.

I used the "Theme information" found in the edit screen of my view to determine the most specific .tpl for "Row style output" to create and created it, in this case view-view-fields--my-view-name--default.tpl.php.

view-view-fields--my-view-name--default.tpl.php - Row output .tpl file
(No longer making use of the default foreach because instead of looping over the fields I know the fields I want and I can simply output them anyway I see fit)

   <div id="homepage-folio-portlets">
    <div class="homepage-folio-portlet <?php print $fields['CSS_class']->content ?>">
     <div class="homepage-folio-portlet-image"><img src="<?php print $fields['Image']->content ?>" width="450" height="330" alt="" class="<?php print $fields['CSS_class']->content ?>-image" /></div>
     <div class="homepage-folio-portlet-text">
      <strong><?php print $fields['Title']->content ?></strong>
      <p><?php print $fields['Body']->content ?></p>
     </div>
    </div> <!-- /homepage-folio-portlet -->
   </div> <!-- /homepage-folio-portlets -->

After that, I did a little recursion into the "Style output" and "Display output" .tpl files to get rid of all that extra markup Drupal adds. Note that all I really cared about was printing out $row (with it's foreach loop) in style .tpl and $rows in the display tpl. It's outputting EXACTLY what I want and I couldn't be happier. Finally, it's making some sense. Hopefully this helps a bunch of other people.

Just for reference...

views-view-unformatted--my-view-name--default.tpl.php - Style .tpl file
(Want to keep the foreach loop in here so each row gets outputted)

<?php foreach ($rows as $id => $row): ?>
 <?php print $row; ?>
<?php endforeach; ?>

views-view--my-view-name--default.tpl.php - Display .tpl file

<?php print $rows; ?>

By removing all the extra markup I am losing important stuff specific to views like admin links and such, but for my purposes that's fine.

初心未许 2024-10-13 11:18:51

在视图的“编辑”选项卡上的“基本设置”下,查找“主题:”,然后单击“信息”链接。然后在“默认:主题信息”部分中,粗体文件名是当前用于主题该视图的特定子部分的文件名。其他名称是“建议”,可用于覆盖默认值,它们按从最不具体到最具体的顺序排列。

在您的情况下,首先,听起来您想要覆盖“行样式输出”:

  1. 单击“行样式输出”链接,复制默认模板代码。
  2. 根据您是否希望将此样式用于所有视图、此视图、此视图的特定显示等,选择用于行样式的建议文件名之一。
  3. 将步骤 #1 中复制的代码粘贴到文件名中在步骤 #2 中选择
  4. 根据需要编辑代码,以添加特定类
  5. 单击“重新扫描模板文件”以重建模板缓存
  6. 对于要自定义的任何其他子模板,请重复步骤 1-5。

On the Edit tab for your view, under Basic Settings, look for "Theme:" and click on the "Information" link. Then in the "Default: Theming information" section, the bold filenames are the ones currently being used to theme a particular sub-section of that view. The other names are "suggestions" that can be used to override the defaults and they are ordered from least specific to most specific.

In your case, to start, it sounds like you want to override the "Row style output":

  1. Click on the "Row style output" link, copy the default template code.
  2. Choose one of the suggested filenames to use for the row style, based on whether you want this style to be used for all views, this view, a particular display of this view, etc.
  3. Paste the code copied in Step #1 into the filename chosen in Step #2
  4. Edit the code as necessary, to add the specific classes
  5. Click on the "Rescan template files" to rebuild the template cache
  6. Repeat steps 1-5 for any additional sub-templates you want to customize.
一场信仰旅途 2024-10-13 11:18:51

您是否尝试过使用行模板文件?您应该在视图模块(在管理中)中看到它。创建视图时,单击左侧下框中的“信息”。您将需要刷新模板缓存(您将看到一个按钮来执行此操作)。

Have you tried using the template files for rows? You should see it in the views module (in the admin). By clicking on "Information" in the lower box on the left side when you are creating the view. You will need to refresh the template cache (you will see a button to do this).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文