如何在表达式引擎 2 中生成唯一 ID?

发布于 2024-12-17 07:00:15 字数 68 浏览 5 评论 0原文

是否有可生成唯一 ID 的 EE2 标签?或者我是否需要嵌入 PHP uniqid() 调用来获取所需的唯一 ID?谢谢。

Is there an EE2 tag that produces a unique ID? Or would I need to embed a PHP uniqid() call to get the desired unique ID? Thanks.

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

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

发布评论

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

评论(2

反话 2024-12-24 07:00:15

不,没有 EE 标签可以做到这一点。它需要您创建自己的插件、扩展或模块。但这很简单。

我的建议是创建一个插件

expressionengine/third_party 文件夹中创建一个名为 guid 的文件夹。
在该文件夹中,创建一个名为 pi.guid.php 的文件,其中包含以下内容:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$plugin_info = array(
    'pi_name'       => 'Uniqid',
    'pi_version'        => '0.1',
    'pi_author'     => 'John Doe',
    'pi_author_url'     => 'http://example.org/',
    'pi_description'    => 'Returns uniqid() with parameters',
    'pi_usage'      => Guid::usage()
);


class Guid {
            
    public function __construct()
    {
        $this->EE =& get_instance();
    }
    
    public function uniqid()
    {
        $prefix = $this->EE->TMPL->fetch_param('prefix');
        $more_entropy = (strtolower($this->EE->TMPL->fetch_param('more_entropy')) == "true") ? TRUE : FALSE;
        
        return uniqid($prefix, $more_entropy);
    }
    
    public static function usage()
    {
        ob_start();  ?>

        Simple use:

    {exp:guid:uniqid}

        Parameter use:

    {exp:guid:uniqid prefix="yourprefix"}
    {exp:guid:uniqid more_entropy="true"}
    {exp:guid:uniqid prefix="yourprefix" more_entropy="true"}
    <?php
        $buffer = ob_get_contents();
        ob_end_clean();

        return $buffer;
    }    
}

好了,您自己的插件可以通过标签创建 uniqid()。
用途?

{exp:guid:uniqid prefix="yourprefix"}
{exp:guid:uniqid more_entropy="true"}
{exp:guid:uniqid prefix="yourprefix" more_entropy="true"}

太棒了,对吧?
我爱EE...

No, there is not a EE tag that does that. It would require that you created your own plugin, extension or module. But that pretty simple.

My suggestion is to create a plugin.

Create a folder named guid in your expressionengine/third_party folder.
In that folder, create a file called pi.guid.php with the following content:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$plugin_info = array(
    'pi_name'       => 'Uniqid',
    'pi_version'        => '0.1',
    'pi_author'     => 'John Doe',
    'pi_author_url'     => 'http://example.org/',
    'pi_description'    => 'Returns uniqid() with parameters',
    'pi_usage'      => Guid::usage()
);


class Guid {
            
    public function __construct()
    {
        $this->EE =& get_instance();
    }
    
    public function uniqid()
    {
        $prefix = $this->EE->TMPL->fetch_param('prefix');
        $more_entropy = (strtolower($this->EE->TMPL->fetch_param('more_entropy')) == "true") ? TRUE : FALSE;
        
        return uniqid($prefix, $more_entropy);
    }
    
    public static function usage()
    {
        ob_start();  ?>

        Simple use:

    {exp:guid:uniqid}

        Parameter use:

    {exp:guid:uniqid prefix="yourprefix"}
    {exp:guid:uniqid more_entropy="true"}
    {exp:guid:uniqid prefix="yourprefix" more_entropy="true"}
    <?php
        $buffer = ob_get_contents();
        ob_end_clean();

        return $buffer;
    }    
}

There you go, your very own plugin to create uniqid() through tags.
The use?

{exp:guid:uniqid prefix="yourprefix"}
{exp:guid:uniqid more_entropy="true"}
{exp:guid:uniqid prefix="yourprefix" more_entropy="true"}

Awesome, right?
I love EE...

心碎的声音 2024-12-24 07:00:15

没有内置的EE标签来输出唯一的ID,没有。

There is no built-in EE tag to output a unique ID, no.

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