• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 1.4 String manipulation in templates

#1
Is there a way to do string manipulation (indexOf, subStr, etc.) when using <xen:set>?

For example:
<xen:set var="$test_content">{xen:raw $firstPost.message}' /></xen:set>
Then check $test_content to see if there is an IMG tag present, and if so, get the index of that tag.
Then do the same for the /IMG tag.
Then do a substr using the location of those two tags to get the value that is between the tags.
 
Last edited:
#2
I figured out how to do what I wanted. This is for using Twitter Cards using the text and image from the first post of a given thread.

I made two files in the /library/TwitterCard folder.

index.php - This checks for an image in the first post of a thread to see if there is an image (assumes only 1 image). It also adds an og;image tag for the image if it finds one (useful for facebook sharing).
<?php

class TwitterCard_index
{
public static function getHTML()
{
$imgString = func_get_arg(1);

$imgStart = strpos($imgString, "");

if ($imgStart === false)
{
print '<meta name="twitter:image:src" content="http://MYSITE/styles/default/xenforo/DEFAULT.jpg">';
}
else
{
$imgStart = $imgStart + 5;
$imgEnd = strpos($imgString, "");
$imgLen = $imgEnd - $imgStart;
$imgURL = substr($imgString, $imgStart, $imgLen);
print '<meta name="twitter:image:src" content="' . $imgURL . '">';
print '<meta property="og:image" content="' . $imgURL . '">';
}

}
}

?>
descr.php - This one strips the IMG tag out of the description, if one exists
<?php

class TwitterCard_descr
{
public static function getHTML()
{
$descString = func_get_arg(1);

$descStart = strpos($descString, "");

if ($descStart === false)
{
print '<meta name="twitter:description" content="' . $descString . '">';
}
else
{
$descClean = substr($descString, $descStart+5);
print '<meta name="twitter:description" content="' . $descClean . '">';
}

}
}

?>
I then modified the $head.description portion of the thread_view template
<xen:container var="$head.description">
<meta name="description" content="{xen:helper snippet, $firstPost.message, 155}" />
<xen:if is="{$firstPost.is_staff} == 1">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@MYTWITTER">
<meta name="twitter:creator" content="@MYTWITTER">
<meta name="twitter:title" content="{$thread.title}">
<xen:callback class="TwitterCard_descr" method="getHTML" params="{xen:helper snippet, $firstPost.message, 155}"></xen:callback>
<xen:callback class="TwitterCard_index" method="getHTML" params="{xen:raw $firstPost.message}"></xen:callback>
</xen:if>
</xen:container>
End result, anything an admin/moderator posts will have the included meta tags for a valid Twitter Card.[/quote]