简体   繁体   English

为什么drupal主题如此困难? 有关简化或更快学习的提示吗?

[英]Why is drupal theming so difficult? Any tips for simplifying it or learning it faster?

I've been coding in PHP for 4 years and even I find it so convoluted and hard to grasp. 我已经用PHP编写了4年的代码,甚至我发现它如此复杂且难以理解。 I have no idea how they expect designers to manage theming. 我不知道他们希望设计师如何管理主题。

I know basic theming, theming with tpl files, views templates, contemplate templates etc. However I get lost whenever I enter the template.php file. 我知道基本的主题,主题是tpl文件,视图模板,考虑模板等。但是每当我输入template.php文件时我都会迷路。

I needed to theme a node input form for a custom content type. 我需要为自定义内容类型设置节点输入表单的主题。 I wracked my brain over it with like 7-9 tutorials and it's still not coming together. 我用7-9个教程将我的大脑震撼了它,它仍然没有聚集在一起。 I understand why module building can be complicated - because most people who work with modules are developers anyway. 我理解为什么模块构建可能很复杂 - 因为大多数使用模块的人都是开发人员。

However, when even a developer can't understand theming , it's definitely over-engineered! 然而,即使开发人员无法理解主题 ,它肯定是过度设计的!

Maybe it's my fault, maybe I didn't learn Drupal properly. 也许这是我的错,也许我没有正确地学习Drupal。 In that case, is there a step-by-step system to becoming a Drupal guru? 在这种情况下,是否有一个逐步的系统成为Drupal大师?

Theme is really not that difficult, if you only know basic php. 主题真的不是那么难,如果你只知道基本的PHP。 You have 3 ways of altering the markup, the css and js, you should be able to handle. 你有三种方法可以改变你应该能够处理的标记,即css和js。

  • Template files . 模板文件 By creating a template file in your theming and naming it correctly, it will take precedence over other template files, and you can thus create custom markup for views, nodes etc. By creating a template for a noce type, you can do stuff like printing out the cck fields in any other instead of using $content . 通过在主题中创建模板文件并正确命名,它将优先于其他模板文件,因此您可以为视图,节点等创建自定义标记。通过为noce类型创建模板,您可以执行诸如打印之类的操作在任何其他字段中输出cck字段而不是使用$content All you need is on the node object. 您只需要在节点对象上。

  • Preprocess functions , which are placed in your theme, will give you the possibility to add or alter variables that will be used in your template. 放置在主题中的预处理函数将使您可以添加或更改将在模板中使用的变量。 You name the functions like hooks: yourtheme_page for the page template yourtheme_node for the node template etc. Here you can create some custom logic, or modifications, to help make your templates more clean and just handle the printing of the variables. 您将函数命名为hooks: yourtheme_page用于页面模板yourtheme_node用于节点模板等。在这里,您可以创建一些自定义逻辑或修改,以帮助使模板更加干净,并只处理变量的打印。

  • Theme functions . 主题功能 You can overwrite theme functions to alter the markup that's used. 您可以覆盖主题函数以更改使用的标记。 You only need to create a function called yourtheme_[theme_function_name] . 您只需要创建一个名为yourtheme_[theme_function_name]的函数。 You don't need to understand much php to do this. 你不需要了解太多的PHP来做到这一点。 Often, you can just copy the original theme function, and make a few alterations in it to get the markup you want. 通常,您只需复制原始主题功能,并对其进行一些更改即可获得所需的标记。

With the above you can do 90-95% of what you need. 通过上述内容,您可以完成所需的90-95%。 Forms are a bit special since you in Drupal 6 can't alter them (easily) in your theme. 表格有点特别,因为你在Drupal 6中无法在主题中轻松改变它们。 Instead what you want is to create a small module and use hook_form_alter , which allows you to modify the form, text used on buttons etc. This is changed in Drupal 7, which will be even easier to theme. 相反,你想要的是创建一个小模块并使用hook_form_alter ,它允许你修改形式,按钮上使用的文本等。这在Drupal 7中更改,这将更容易主题。

So it really isn't all that complicated, just use the 3 basic tools described above. 所以它真的不是那么复杂,只需使用上面描述的3种基本工具。 The tricky part can be to name your functions and templates, but devel themer can help you with that. 棘手的部分可能是命名你的函数和模板,但devel themer可以帮助你。 Another tool you use is devel which can assist you in printing out variables so you can inspect them and see what you have available, fx CCK fields on the node. 您使用的另一个工具是devel ,它可以帮助您打印变量,以便您可以检查它们并查看您可用的内容,节点上的fx CCK字段。

Maybe you should get a copy of Front End Drupal . 也许你应该得到前端Drupal的副本。

In my opinion, Drupal theming is not difficult once you wrap your head around the concepts like templates, overrides, .info files and preprocess functions. 在我看来,一旦你绕过模板,覆盖,.info文件和预处理函数等概念,Drupal主题就不难了。 Those things are not directly related to php skills, it's all about understanding the system. 这些东西与php技能没有直接关系,而是与理解系统有关。

It's interesting to see that you're a themer, yet you only talk about php, not CSS and HTML. 有趣的是看到你是一个人,但你只谈论PHP,而不是CSS和HTML。 In my experience, a lot of theming tasks can be accomplished with CSS, without even touching php. 根据我的经验,很多主题任务都可以用CSS完成,甚至没有触及php。

Finally, I don't know if you are using a base/starter theme (like Zen or Genesis ) already. 最后,我不知道你是否已经使用了基础/入门主题 (如ZenGenesis )。 I recommend using a base theme and realizing your own design as a sub theme. 我建议使用基本主题并将您自己的设计实现为子主题。

Most people get an 'ahah!' 大多数人得到'啊哈!' moment when themeing suddenly starts to make sense. 主题突然开始变得有意义的时刻。

I'd argue that this is probably tougher for experienced PHP developers, since you have to get past a lot of concepts that don't make sense in vanilla PHP ("the function is magically called at the appropriate time ... because of how it's named ?!") 我认为这对于有经验的PHP开发人员来说可能更难,因为你必须通过许多在vanilla PHP中没有意义的概念(“这个函数在适当的时候被神奇地调用...因为如何它被命名了 ?!“)

@googletorp's answers is pretty comprehensive, so I'll just add some practical tips that helped me along: @ googletorp的答案非常全面,所以我只想添加一些帮助我的实用技巧:

1) Try building a module that implements it's own themeable output and simple hooks. 1)尝试构建一个模块,实现它自己的主题输出和简单的钩子。 Hooks and theme functions make a lot more sense when you see how modules are actually calling them and using them. 钩和主题功能使很多更有意义,当你看到模块实际上是如何打电话给他们,并使用它们。

2) Make liberal use of the devel module, especially the Theme Registry menu (note how it changes when you add a new theme function), the 'Render' tab, and the dpm() and dvm() functions 2)自由使用devel模块,特别是Theme Registry菜单(注意添加新主题函数时它如何变化),'Render'选项卡以及dpm()dvm()函数

3) Buy a old-tyme dead-tree book on Drupal themeing. 3)购买一本关于Drupal主题的古老的死树书。 The online docs are really outstanding if you already know what you're doing, but can be extremely confusing if you're not sure what you're looking for. 如果您已经知道自己在做什么,那么在线文档非常出色,但如果您不确定自己在寻找什么,则可能会非常混乱。

4) Empty your cup. 4)清空你的杯子。 At the theme level, PHP is really just for simple logic and syntax -- almost everything of substance is getting handled by Drupal API functions. 在主题级别,PHP实际上只是用于简单的逻辑和语法 - 几乎所有实质内容都由Drupal API函数处理。 Try to think like a Drupal developer and not a PHP developer (ie don't assume you know how to do something just because you built a vanilla PHP site that does the same thing) and you'll have an easier time. 尝试像Drupal开发人员一样思考而不是PHP开发人员(也就是说,不要因为你构建了一个同样的东西的vanilla PHP网站而假设你知道如何做某事)而且你会有更轻松的时间。

Keep with it! 坚持下去! The theme system is actually really easy to use once you've figured it out. 一旦你弄明白,主题系统实际上很容易使用。

to answer your question on why it is so hard : 回答你为什么这么难的问题

Drupal themes may seem overwhelming, due to the sheer amount of possibilities, see @googletorps answer for a good overview. Drupal主题可能看起来势不可挡,由于可能性很大,请参阅@googletorps回答以获得良好的概述。 Having "many ways to do one thing" brings power: power users can choose the best of all possibilities. “多种方式做一件事”带来了力量:高级用户可以选择最好的可能性。 But it also brings complexity: new users don't get a good lead, because there is no "you should do it this way. fullstop." 但它也带来了复杂性:新用户没有获得良好的领先优势,因为没有“你应该这样做。完全停止。”

Alongside that amount of ways to get stuff done, there is the problem of nesting. 除了那些完成工作的方法之外,还有嵌套问题。 Drupal has a concept of very deeply nested items. Drupal有一个非常深层嵌套项目的概念。 To take a random, yet simple example: 采取一个随机但简单的例子:

username<menuitem<menuitem<menuitem<menu<block<region<page. 

Will render a menu-item, containing a username in a three level deep menu-item in a sidebar-block. 将呈现一个菜单项,其中包含侧边栏中三级深层菜单项中的用户名。 Where most templating-environments have concepts of a page, containing several "contents" that, at most, might contain some partials. 大多数模板环境都有页面概念,其中包含多个“内容”,最多可能包含一些部分内容。 the nesting is at most three levels deep. 嵌套最多是三层。 And each level has a clear, distinct area of expertise. 每个级别都有明确,独特的专业领域。 Drupal does not have that: the nesting is fairly arbitrary. Drupal没有:嵌套是相当随意的。 And each nested item is no different from its parents. 每个嵌套项与其父项没有区别。 This, again, offers power-users a great concept and power to work with, but is hard to grok for new developers/designers. 这再一次为高级用户提供了一个很好的概念和强大的功能,但很难为新的开发人员/设计人员提供帮助。

Lastly, another reason why it is hard to learn, is that the Drupal online documentation is more a wiki then a read-from-begin-to-end manual. 最后,另一个难以学习的原因是,Drupal在线文档更像是wiki,而不是从头到尾阅读的手册。 There are great books that fill htat gap, you will have to buy them, though. 虽然有很好的书籍可以填补空白,你必须购买它们。

If you haven't seen it already, this is a great presentation on why Drupal 6 theming is difficult and how it is improved in Drupal 7. There is hope! 如果您还没有看过它,这是一个很好的演示文稿,说明为什么Drupal 6主题难以以及如何在Drupal 7中进行改进。有希望! The video made more sense to me a second time watching after more experience theming. 经过更多的体验后,这段视频对我来说更有意义。 http://sf2010.drupal.org/conference/sessions/design-and-theming-whats-new-drupal-7 http://sf2010.drupal.org/conference/sessions/design-and-theming-whats-new-drupal-7

One of the biggest tricks is to override something that is normal rendered with a theme function into a template. 其中一个最大的技巧是将使用主题函数正常渲染的内容覆盖到模板中。 This gives you much more control over the markup, and also lets you use the preprocess functions to manipulate the variables before handing it over to the template. 这使您可以更好地控制标记,还可以使用预处理函数在将变量移交给模板之前对其进行操作。

This is a huge boon to theming forms. 这对于主题形式来说是一个巨大的好处。 See a short video that explains it better than I could here: http://drupaldojo.com/session/fine-tuning-ui-theming-forms-drupal-60 观看一个简短的视频,比我在这里更好地解释它: http//drupaldojo.com/session/fine-tuning-ui-theming-forms-drupal-60

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM