Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Twitter 使用JSON抛出的Hugo短代码;坏字符U+;0022错误“;_Twitter_Hugo_Hugo Shortcode - Fatal编程技术网

Twitter 使用JSON抛出的Hugo短代码;坏字符U+;0022错误“;

Twitter 使用JSON抛出的Hugo短代码;坏字符U+;0022错误“;,twitter,hugo,hugo-shortcode,Twitter,Hugo,Hugo Shortcode,我正在尝试使用两个参数创建一个短代码,一个CSS类和一个Tweet ID。我的出发点是这个示例,它展示了如何使用默认的Hugo Twitter短代码 {{} 但是我想在post.md中这样使用它: {{} 要生成html,请执行以下操作: <div class="tweet-in-post alignright"> <twitter-widget class=.... </div> 给出错误错误字符U+0022' 这些文档展示了如何在Hugo中调用JSON函数,但

我正在尝试使用两个参数创建一个短代码,一个CSS类和一个Tweet ID。我的出发点是这个示例,它展示了如何使用默认的Hugo Twitter短代码

{{}

但是我想在
post.md
中这样使用它:

{{}

要生成html,请执行以下操作:

<div class="tweet-in-post alignright">
<twitter-widget class=....
</div>
给出错误
错误字符U+0022'


这些文档展示了如何在Hugo中调用JSON函数,但我不知道如何使用这些示例来实现我的短代码。

定制内置短代码的最佳方法是从代码开始

您提供的链接显示了如何使用tweet短代码,但没有显示其代码的实际外观。需要深入Hugo源代码才能真正找到内置短代码的外观

您可以在此处找到它们:

在当前提交(
67f9204
)时,twitter短代码是这样的:

{{-$pc:=.Page.Site.Config.Privacy.Twitter-}
{{-如果不是$pc.Disable-}
{{-if$pc.Simple-}
{{template“\u internal/shortcode/twitter\u simple.html”。}
{{-else-}
{{-$url:=printf“https://api.twitter.com/1/statuses/oembed.json?id=%v&dnt=%t“(index.Params 0)$pc.EnableDNT-}
{{-$json:=getJSON$url-}
{{$json.html | safeHTML}
{{-end-}
{{-end-}
这就是你的出发点

但是这个例子特别棘手,因为这个短代码使用了由twitterapi设置的样式,特别难以处理

我可以帮你

这里的另一个困难是,如果您查看twitter短代码,您可以看到tweet ID不是由
{.Get“ID”}
(命名参数)调用的,而是由
(index.params 0)
(位置参数)调用的。Hugo不允许您混合使用命名参数和位置参数

因此,您不能在尝试时使用类似于
{.Get“class”}
的内容。相反,您需要使用位置参数,例如
{.Get xx}
xx
作为您的参数在短代码调用中的位置

这里有一个可能的解决方案:

{{-$pc:=.Page.Site.Config.Privacy.Twitter-}
{{-如果不是$pc.Disable-}
{{-if$pc.Simple-}
{{template“\u internal/shortcode/twitter\u simple.html”。}
{{-else-}
{{-$url:=printf“https://api.twitter.com/1/statuses/oembed.json?id=%v&dnt=%t“(索引参数1)$pc.EnableDNT-}
{{-$json:=getJSON$url-}
{{$json.html | safeHTML}
{{-end-}
{{-end-}
正如你所看到的,我在上面提到的帖子的答案之后添加了一些
定制,我使用
{.Get 0}
作为位置。这意味着我现在必须将tweet ID的参数位置调整为
1

如果您将此短代码命名为tweet single.html,则可以使用以下代码调用它:

{{}
将推文放到右边

要将推文置于中心位置,需要:

{{}
请注意,
right
center
不带引号,因为它们是位置参数


替代方法:

如果由于某种原因,这种方法不适用于您,那么一种完全不同的方法将是重新创建tweet的整个格式(而不使用twitterapi的功能)

如果您使用
twitter\u simple.html
而不是
twitter.html
,Hugo就是这么做的

如果查看
twitter\u simple.html
,您会发现他们忽略了样式脚本,而是为
.twitter tweet
类重新创建了一些更简单的格式:

{{-$pc:=.Page.Site.Config.Privacy.Twitter-}
{{-$sc:=.Page.Site.Config.Services.Twitter-}
{{-如果不是$pc.Disable-}
{{-$id:=.Get 0-}
{{-$json:=getJSON“https://api.twitter.com/1/statuses/oembed.json?id=“$id”&省略_script=true“-}
{{-如果不是$sc.DisableInlineCSS-}
{{template“\uuuh\usimple\utwitter\ucss”$}
{{-end-}
{{$json.html | safeHTML}
{{-end-}
{{define“{uuuh\usimple\utwitter\ucss}}
{{如果没有(.Page.Scratch.Get“\uu h\u simple\u twitter\u css”)}
{{/*仅包含一次*/}
{{.Page.Scratch.Set“{u h_simple\u twitter\u css”true}
.推特推特{
字体:14px/1.45-苹果系统、BlinkMacSystemFont、“Segoe UI”、Roboto、Oxygen Sans、Ubuntu、Cantarell、“Helvetica Neue”、无衬线;
左边框:4px实心#2b7bb9;
左侧填充:1.5em;
颜色:#555;
}
.推特推特{
颜色:#2b7bb9;
文字装饰:无;
}
blockquote.twitter-tweet a:悬停,
blockquote.twitter-tweet a:关注{
文字装饰:下划线;
}
{{end}
{{end}

您可以使用此代码,直到有适合您的自定义项为止。

自定义内置短代码的最佳方法是从其代码开始

您提供的链接显示了如何使用tweet短代码,但没有显示其代码的实际外观。需要深入Hugo源代码才能真正找到内置短代码的外观

您可以在此处找到它们:

在当前提交(
67f9204
)时,twitter短代码是这样的:

{{-$pc:=.Page.Site.Config.Privacy.Twitter-}
{{-如果不是$pc.Disable-}
{{-if$pc.Simple-}
{{template“\u internal/shortcode/twitter\u simple.html”。}
{{-else-}
{{-$url:=printf“https://api.twitter.com/1/statuses/oembed.json?id=%v&dnt=%t“(index.Params 0)$pc.EnableDNT-}
{{-$json:=getJSON$url-}
{{$json.html | safeHTML}
{{-end-}
{{-end-}
这就是你的出发点

但是这个例子特别棘手,因为这个短代码使用了twitter api和
<!-- tweet-single -->

<div class="tweet-in-post {{ .Get "class" }}">

{{ (getJSON "https://api.twitter.com/1.1/statuses/oembed.json?dnt=1&hide_thread=1&id={{ .Get "id" }}") }}

</div>