Yii注册JS变量

时间:2015-01-25 21:40:40

标签: javascript php yii

我正在使用基于Yii的HumHub,并尝试使用从丰富文本的函数中提取的URL设置JS变量。

目前,该变量似乎没有在模型中设置,所以我甚至还没有真正开始使用该脚本。

它应该最终获取OpenGraph数据,但我甚至无法获得我打算调试和使用的脚本的URL。

Base enrichText功能

   /**
     * Converts an given Ascii Text into a HTML Block
     * @param boolean $allowHtml transform user names in links
     * @param boolean $allowEmbed Sets if comitted video links will embedded
     *
     * Tasks:
     *      nl2br
     *      oembed urls
     */
    public static function enrichText($text, $from = 'default', $postid = '')
    {

        if ( $from == 'default' ) {
            $maxOembedCount = 3; // Maximum OEmbeds
            $oembedCount = 0; // OEmbeds used

            // Parse bbcodes before link parsing
            $text = self::parseBBCodes($text);

            $text = preg_replace_callback('/(?<!\])(https?:\/\/.*?)(\s|$)(?!\[)/i', function ($match) use (&$oembedCount, &$maxOembedCount) {

                // Try use oembed
                if ($maxOembedCount > $oembedCount) {
                    $oembed = UrlOembed::GetOembed($match[0]);
                    if ($oembed) {
                        $oembedCount++;
                        return $oembed;
                    }
                }

                $regurl = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

                // Check if there is a url in the text independently and render to JS var
                if(preg_match($regurl, $text, $url)) {

                    if (!empty($postid)) {      
                        Yii::app()->clientScript->setJavascriptVariable("ourl".$postid, $url[0]);
                    }

                }

                return HHtml::link($match[1], $match[1], array('target' => '_blank')).$match[2];

            }, $text);

            // get user and space details from guids
            $text = self::translateMentioning($text, true);

            // create image tag for emojis
            $text = self::translateEmojis($text);

            return nl2br($text);

        } else {

            // Parse bbcodes before link parsing
            $text = self::parseBBCodes($text, $from);

            return $text;

        }
    }

致电信息

<?php
/**
 * This view represents a wall entry of a post.
 * Used by PostWidget to show Posts inside a wall.
 *
 * @property User $user the user which created this post
 * @property Post $post the current post
 *
 * @package humhub.modules.post
 * @since 0.5
 */
?>
<div class="panel panel-default post" id="post-<?php echo $post->id; ?>">
    <div class="panel-body">
        <?php $this->beginContent('application.modules_core.wall.views.wallLayout', array('object' => $post)); ?>
        <span id="post-content-<?php echo $post->id; ?>" style="overflow: hidden; margin-bottom: 5px;">
            <?php print HHtml::enrichText($post->message, 'default', $post->id); ?>
        </span>
        <a class="more-link-post hidden" id="more-link-post-<?php echo $post->id; ?>" data-state="down"
           style="margin: 20px 0 20px 0;" href="javascript:showMore(<?php echo $post->id; ?>);"><i
                class="fa fa-arrow-down"></i> <?php echo Yii::t('PostModule.widgets_views_post', 'Read full post...'); ?>
        </a>
        <div id="opengraph-<?php echo $post->id; ?>" class="opengraph-container">
            <div class="opengraph-img-<?php echo $post->id; ?>"></div>
            <div class="opengraph-body">
                <h2 class="opengraph-heading-<?php echo $post->id; ?>"></h2>
                <div class="opengraph-content-<?php echo $post->id; ?>"></div>
            </div>
        </div>
        <?php $this->endContent(); ?>
    </div>
</div>


<script type="text/javascript">


    console.log('Oembed URL for <?php echo $post->id; ?>: '+ourl<?php echo $post->id; ?>);

// ... etc

更新我能够通过在文本末尾添加脚本来传递变量。非常非常脏的方法。我希望有更清洁的东西。 :(

   /**
     * Converts an given Ascii Text into a HTML Block
     * @param boolean $allowHtml transform user names in links
     * @param boolean $allowEmbed Sets if comitted video links will embedded
     *
     * Tasks:
     *      nl2br
     *      oembed urls
     */
    public static function enrichText($text, $from = 'default', $postid = '')
    {

        if ( $from == 'default' ) {
            $maxOembedCount = 3; // Maximum OEmbeds
            $oembedCount = 0; // OEmbeds used

            // Parse bbcodes before link parsing
            $text = self::parseBBCodes($text);

            $text = preg_replace_callback('/(?<!\])(https?:\/\/.*?)(\s|$)(?!\[)/i', function ($match) use (&$oembedCount, &$maxOembedCount) {

                // Try use oembed
                if ($maxOembedCount > $oembedCount) {
                    $oembed = UrlOembed::GetOembed($match[0]);
                    if ($oembed) {
                        $oembedCount++;
                        return $oembed;
                    }
                }

                return HHtml::link($match[1], $match[1], array('target' => '_blank')).$match[2];

            }, $text);

            // get user and space details from guids
            $text = self::translateMentioning($text, true);

            // create image tag for emojis
            $text = self::translateEmojis($text);

            $regurl = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

            // Check if there is a url in the text independently and render to JS var
            if(preg_match($regurl, $text, $url)) {

                if (!empty($postid)) {      
                    $text .= '<script type="text/javascript"> var ourl'.$postid.' = \''.$url[0].'\'; </script>';
                }

            }

            return nl2br($text);

        } else {

            // Parse bbcodes before link parsing
            $text = self::parseBBCodes($text, $from);

            return $text;

        }
    }

更新2:尝试获取Opengraph数据

这里我尝试从HHtml::enrichText()返回中的假设设置变量中获取opengraph中的数据。但是我收到错误:SyntaxError: expected expression, got '<' jquery.js:1指向jquery文件的第一行,这是脚本的注释和许可证。

该脚本也没有在源代码中显示

<?php
/**
 * This view represents a wall entry of a post.
 * Used by PostWidget to show Posts inside a wall.
 *
 * @property User $user the user which created this post
 * @property Post $post the current post
 *
 * @package humhub.modules.post
 * @since 0.5
 */
?>
<div class="panel panel-default post" id="post-<?php echo $post->id; ?>">
    <div class="panel-body">
        <?php $this->beginContent('application.modules_core.wall.views.wallLayout', array('object' => $post)); ?>
        <span id="post-content-<?php echo $post->id; ?>" style="overflow: hidden; margin-bottom: 5px;">
            <?php print HHtml::enrichText($post->message, 'default', $post->id); ?>
        </span>
        <a class="more-link-post hidden" id="more-link-post-<?php echo $post->id; ?>" data-state="down"
           style="margin: 20px 0 20px 0;" href="javascript:showMore(<?php echo $post->id; ?>);"><i
                class="fa fa-arrow-down"></i> <?php echo Yii::t('PostModule.widgets_views_post', 'Read full post...'); ?>
        </a>
        <div id="opengraph-<?php echo $post->id; ?>" class="opengraph-container">
            <div class="opengraph-img-<?php echo $post->id; ?>"></div>
            <div class="opengraph-body">
                <h2 class="opengraph-heading-<?php echo $post->id; ?>"></h2>
                <div class="opengraph-content-<?php echo $post->id; ?>"></div>
            </div>
            <script type="text/javascript">
                $(document).ready(function(){
                    (function() {
                        var opengraph = "http://bfxsocial.strangled.net/resources/Opengraph/getInfo.php?callback=?";
                        $.getJSON( opengraph, {
                            href: ourl<?php echo $post->id; ?>,
                            format: "json"
                        })
                        .done(function( data ) {
                            console.log('<?php echo Yii::t('PostModule.widgets_views_post', 'Opengraph: Response from: '); ?>'+ourl-<?php echo $post->id; ?>+"\n\n"+data);
                            var img = $('<img />',{ id: 'og:img-<?php echo $post->id; ?>', src: data['og:image'], alt:'data.title'}).appendTo($('.opengraph-img-<?php echo $post->id; ?>'));
                            $('.opengraph-heading-<?php echo $post->id; ?>').html(data.title);
                            $('.opengraph-body-<?php echo $post->id; ?>').html(data.description);
                            $('#opengraph-<?php echo $post->id; ?>').show();
                        });
                    })();   
                });
            </script>
        </div>
        <?php $this->endContent(); ?>
    </div>
</div>

<!-- Opengraph Temp Style -->
<style type="text/css">
.opengraph-container 
  display: none;
  width: 100%;
  padding: 3px;
  margin: 5px;
  background-color: rgba(0,0,0,0.1);
  border: 1px solid rgba(150,150,150,0.1);
}
.opengraph-img {
  display: block;
  min-width: 99%;
  max-height: 350px;
  margin: 0 auto;
}
.opengraph-body {
  width: 99%;
  padding-top: 5px;
  border-top: 1px solid rgba(0,0,0,0.1);
}
.opengraph-heading {
  display: block;
  width: 250px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.opengraph-content {
  font-size: 12px;
  color: #7F7F7F;
}
</style>
<!-- End: Opengraph Temp Style -->

0 个答案:

没有答案