如何获取当前drupal主题的路径?

发布于 2024-07-08 20:04:10 字数 190 浏览 5 评论 0原文

The Drupal API has drupal_get_path($type, $name) which will give the path of any particular theme or module. What if I want the path of the current theme?

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

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

发布评论

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

评论(9

雪花飘飘的天空 2024-07-15 20:04:10

使用 path_to_theme 函数。

Use the path_to_theme function.

陌上青苔 2024-07-15 20:04:10

这应该有效(doc):

global $theme;
$path = drupal_get_path('theme', $theme);

// there's also a $theme_path global

global $theme_path;

this should work (doc):

global $theme;
$path = drupal_get_path('theme', $theme);

// there's also a $theme_path global

global $theme_path;
埖埖迣鎅 2024-07-15 20:04:10

在 D6 中,path_to_theme() 可能不会按照您期望的方式运行,具体取决于您使用它的方式。 如果您在任何主题预处理函数之外使用它,那么它可能会给您想要的东西,但是如果在模块的主题/预处理挂钩函数的上下文中调用它......它将指向该模块路径宣告了主题。

前任。 如果我有一个主题“my_theme”和我的模块“my_module”,它使用预处理挂钩覆盖论坛主题,则在我的模块中调用path_to_theme():例如my_module_preprocess_forums()...将返回“forums”,而不是“my_theme”正如人们所预料的那样。

如果你问我的话,非常果味。

In D6 path_to_theme() may not behave in a way you expect depending on how you are using it. If you are using it outside any theme preprocess functions, then it will probably give you what you want, but if it is being called within the context of a module's theming/preprocess hook function... it will be pointing to the module path that declared the theme.

Ex. If i have a theme "my_theme" and my module "my_module" which is overriding the forum themes using the preprocess hooks, calling path_to_theme() within my module: e.g. my_module_preprocess_forums()... will return "forums", and not "my_theme" as one might expect.

Very fruity if you ask me.

假面具 2024-07-15 20:04:10

在 Drupal 8 中,如果您需要在管理主题处于活动状态时获取活动主题路径,您可以获取默认主题路径:

$themeHandler = \Drupal::service('theme_handler');
$themePath = $themeHandler->getTheme($themeHandler->getDefault())->getPath();

In Drupal 8, if you need to get the active theme path when you have the admin theme active you can fetch the default theme path:

$themeHandler = \Drupal::service('theme_handler');
$themePath = $themeHandler->getTheme($themeHandler->getDefault())->getPath();
白况 2024-07-15 20:04:10

在 Drupal 7 中,为了获取当前主题的路径,我们可以使用:
path_to_theme() 函数。

In Drupal 7, for getting current theme's path, we can use:
path_to_theme() function.

勿忘初心 2024-07-15 20:04:10

在 Drupal 8 中

global $base_url;
$theme = \Drupal::theme()->getActiveTheme();
$image_url = $base_url.'/'. $theme->getPath() .'/images/image.jpg';

In Drupal 8

global $base_url;
$theme = \Drupal::theme()->getActiveTheme();
$image_url = $base_url.'/'. $theme->getPath() .'/images/image.jpg';
阳光的暖冬 2024-07-15 20:04:10

在 Drupal 5 中,您可以简单地使用: path_to_theme()

这将为您提供从 Drupal 根目录到特定主题目录的完整路径。 请注意,它不包含尾部斜杠。

在 Drupal 6 中,其行为略有不同。 如果您从页面内调用它,它将调用当前正在执行主题的任何内容...无论是您的主题、模块等。以下是 API 文档中的关键引用:

它可以指向活动主题或
处理主题的模块
执行。 例如,当
在主题范围内调用
调用它取决于在哪里
处理主题功能。 如果
从模块实现,它将
指向模块。 如果实施
从活动主题来看,它将指向
到活动主题。 被叫时
在主题调用的范围之外,
它将始终指向活动的
主题。

来源:http://api.drupal.org/api/function/path_to_theme

In Drupal 5, you can simply use: path_to_theme()

This will give you a complete path from the root of Drupal to the specific theme directory. Be aware, it does not include a trailing slash.

In Drupal 6, this behaves just a bit differently. If you call it from within your pages, it will call whatever is currently doing the theming... whether that is your theme, a module, etc. Here's the key quote from the API docs:

It can point to the active theme or
the module handling a themed
implementation. For example, when
invoked within the scope of a theming
call it will depend on where the
theming function is handled. If
implemented from a module, it will
point to the module. If implemented
from the active theme, it will point
to the active theme. When called
outside the scope of a theming call,
it will always point to the active
theme.

Source: http://api.drupal.org/api/function/path_to_theme

纸短情长 2024-07-15 20:04:10

如果您已经知道主题名称

$themePath = \Drupal::service('extension.list.theme')->getPath('my_theme_name');

 $themePath = \Drupal::service('extension.path.resolver')->getPath('theme', 'my_theme_name');

If you already know the theme name

$themePath = \Drupal::service('extension.list.theme')->getPath('my_theme_name');

or

 $themePath = \Drupal::service('extension.path.resolver')->getPath('theme', 'my_theme_name');
近箐 2024-07-15 20:04:10

对于 D8,主题文件夹可在预处理函数中使用:

function hook_preprocess_page(&$variables) {
  $variables['some_logo_file'] = "/{$variables['theme']['path']}/images/logo.png";
}

page.html.twig:

<img src="{{ logo_src }}">

For D8, the theme folder is available in preprocess functions:

function hook_preprocess_page(&$variables) {
  $variables['some_logo_file'] = "/{$variables['theme']['path']}/images/logo.png";
}

page.html.twig:

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