0

I'm working on a PHP project in-which I need to convert a .slim (http://slim-lang.com/) file to regular HTML.

Unfortunately, it seems the only way of doing this is through using Ruby as I can't find any PHP parsers for slim.

However, I'm totally new to ruby and have never even used it before. Anyways, I'm going to have to execute a .rb file from PHP to do the conversion.

However, I'm having trouble setting up ruby to run properly (I believe).

I setup a folder (slim) with the following:

Gemfile

source 'https://rubygems.org'
gem "listen"
gem "slim"
gem "sass"

Gemfile.lock

   GEM
  remote: https://rubygems.org/
  specs:
    ffi (1.9.0)
    listen (1.3.0)
      rb-fsevent (>= 0.9.3)
      rb-inotify (>= 0.9)
      rb-kqueue (>= 0.2)
    rb-fsevent (0.9.3)
    rb-inotify (0.9.1)
      ffi (>= 0.5.0)
    rb-kqueue (0.2.0)
      ffi (>= 0.5.0)
    sass (3.2.10)
    slim (2.0.1)
      temple (~> 0.6.6)
      tilt (>= 1.3.3, < 2.1)
    temple (0.6.6)
    tilt (1.4.1)

PLATFORMS
  ruby

DEPENDENCIES
  listen
  sass
  slim

main.rb

require 'listen'
require 'slim'


# if source folder specified
if ARGV[0]
  Listen.to!(ARGV[0], :filter => /\.slim$/) do |modified, added| 
    modified.each do |modified_file|
      system("slimrb --pretty #{modified_file} > #{modified_file.gsub('.slim','')}.html --trace")
    end
  end
end

So from what I understand, this should get any file with a .slim extension and convert it to .html

Then in my PHP I've got:

<?php 

$pathToRB = "http://example.com/path/to/ruby/file/main.rb";

exec($pathToRB);

?>

Unfortunately, it doesn't do anything though. I'm pretty sure this has something to do with having ruby running on the server or something, but like I said, I'm totally new to it and not sure how to get it up and running.

Any help is much appreciated.

3
  • How do you expect web server to run Ruby script out of itself? Why don;t you use system function or similar? Commented Jun 13, 2014 at 9:22
  • i tried system, still doesn't work. Like I said, pretty sure it's something to do with ruby Commented Jun 13, 2014 at 9:26
  • What did you try? Do you understand what are you doing at all? Commented Jun 13, 2014 at 9:28

1 Answer 1

2

First you have install slim gem in your sever.

gem install slim

Next, run the slimrb command in php.

<?php

$input = "/path/to/foo.slim";
$output = "/path/to/foo.html";

// if this doesn't work, try use the full path of slimrb
exec("slimrb --pretty $input > $output");
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think I have access to console on the server? Could I just download the repo from github and put it in the slim folder?
@user3143218 If your server doesn't have ruby environment, then no way. Just like you don't install php in your server but want to run php code on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.