sbrothy Posted April 4 Share Posted April 4 Although I'm not completely new to PHP it's not my first choice of language so I may still fall in traps from time to time. I'm currently using this newsticker: https://phppot.com/javascript/javascript-news-ticker/ and it's worked like a charm. Recently, I decided to implement some basic I18n and wanted to isolate the small script: <div id="grid-logo-comment" class="rss"> <ul> <li>RSS: <?=$rss["title"]?></li> <li>Description: <?=$rss["description"]?></li> <?php foreach ($rss_feed->channel->item as $feed_item) { ?> <li><a href="<?=$feed_item->link?>"><?=$feed_item->title?></a></li> <?php } ?> </ul> </div> <script>startTicker('grid-logo-comment');</script> like so: [...] $ticker_rss_template = __ROOT__ . "/views/ticker_rss_" . $language . ".php"; [...] <div id="grid-logo-comment" class="rss"> <?php include $ticker_rss_template; ?> </div> <script>startTicker('grid-logo-comment');</script> with the template file being the very simple UL block from above: Quote <ul> <li>RSS: <?=$rss["title"]?></li> <li>Description: <?=$rss["description"]?></li> <?php foreach ($rss_feed->channel->item as $feed_item) { ?> <li><a href="<?=$feed_item->link?>"><?=$feed_item->title?></a></li> <?php } ?> </ul> For some reason, which currently eludes me, this doesn't work. It apparently has to be in there to work. I've tried various things like putting the 'startTicker' function in the <body onload=""> block but so far I've had no luck. What am I missing here? Regards and TIA, sbrothy Quote Link to comment https://forums.phpfreaks.com/topic/332878-javascript-code-not-executed-when-brought-in-by-phps-include/ Share on other sites More sharing options...
sbrothy Posted April 4 Author Share Posted April 4 It seems it's something with timing as the source looks exactly the same when the page is built and shown. Just no ticking. Quote Link to comment https://forums.phpfreaks.com/topic/332878-javascript-code-not-executed-when-brought-in-by-phps-include/#findComment-1663395 Share on other sites More sharing options...
gizmola Posted April 4 Share Posted April 4 A couple of things: There's no javascript in any of your code When you have essential code, don't use include, use require(). There's really no reason to ever use include. I would highly advise the use of alternative syntax when you are doing so much intermingling of html markup and code. You are already using <?= so you might as well use it with all your control structure as well. See https://www.php.net/manual/en/control-structures.alternative-syntax.php I personally add spacing on lines where I use <?= ... ?> as it's more readable to me. Just wanted to point out that it does work if you leave spaces. Here's how you would rewrite your last template snippet (the foreach): <ul> <li>RSS: <?= $rss["title"] ?></li> <li>Description: <?= $rss["description"] ?></li> <?php foreach($rss_feed->channel->item as $feed_item): ?> <li><a href="<?= $feed_item->link ?>"><?= $feed_item->title ?></a></li> <?php endforeach; ?> </ul> You can use string functions, use ternary syntax and null coalesce, and do concatenation and interpolation inside a <?= block as well. Simple example-- <?php $foo = 'Hello'; ?> <div> <?php for($x=1; $x < 6; $x++): ?> <p>I say to you: <?= $x . '.' . strtoupper($foo) ?></p> <?php endfor; ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/332878-javascript-code-not-executed-when-brought-in-by-phps-include/#findComment-1663396 Share on other sites More sharing options...
sbrothy Posted April 4 Author Share Posted April 4 I put it back to how it was before so now it works again. It's a mess yes. I'll find some other way of cleaning it up before it gets out of hand. OTOH it's hardly mission critical. Just my personal plaything. https://omecc.dk/ Quote Link to comment https://forums.phpfreaks.com/topic/332878-javascript-code-not-executed-when-brought-in-by-phps-include/#findComment-1663397 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.